0
ytho
4y

Being new to NodeJS, I wanted to use the framework for a small script that involved connecting to a MySQL database and updating 1500+ records.

With NodeJS's preference towards functional programming over sequential, I wanted to do things the NodeJS way with callback functions instead how I'm used to doing it, using loops (and all the MySQL functions were async).

I couldn't update all the rows at once, so I wrote a callback function that calls back itself after the SQL statement is executed. A recursive callback function... am I doing this right?

Comments
  • 0
    it doesnt have the ability for batchprocessing?
  • 1
    Write a generator to produce paged series of your record for batching to sql:

    https://developer.mozilla.org/en-US...*

    Js doesn't support tail recursion, so you'll either need to flatten the recursion that way, or use the trampoline pattern.

    https://blog.logrocket.com/using-tr...
  • 1
    Sounds cool.
    Take care of edge cases though, we wouldn't want it to have a fucked up exit condition.
  • 0
    @SortOfTested thank you, I'll look into that.
  • 2
    Fuck callbacks. Use mysql2/promise and use async await... That's like the best of both worlds (async but readable like sequential and no callback hell)

    I'd never want to use JS without async await anymore. Just make sure to try catch where appropriate.
  • 1
    @eval
    I would agree with this strongly if I didn't truly prefer Observables to the async await Impl in js.
  • 0
    A resursive callback function like that might cause a stack overflow if the number of recursions are too high.
Add Comment