Monday, 13 March 2017

Correct way to chain multiple async functions on array items with callback

My title is kind of gross. Let me elaborate:

I'm using node 7.2.0 and Electron. I have an array of Song objects with a key called filepath. I need to do the following for each song, in order:

  1. Get their ID3 metadata
  2. Send artist and track info to API
  3. Apply correct track information from API to each object
  4. Sort into albums
  5. Display results

So what I need to know is this: what is the best way to chain aync functions, and how can I implement a callback when the chain completes.

Should I run each song through all the steps as soon as I can? Should I wait until all songs are done step 1, then move to step 2, then step 3 when all are finished step 2, etc.

Secondly, what's the accepted way to run a callback when all the above steps (probably in a forEach loop) finish? Should I use the node async library or promises? I've been reading that I should wrap each singular function in a promise then use Promise.all() to run a callback, but I'm not sure if that's up to date.



via Kieran E

No comments:

Post a Comment