M41 Highway

Data science and software engineering blog

Leave a comment

Synchronous control of iteration containing callback execution

You love Node.js because of its non-blocking programming model to make your software better throughput. Sometimes you need step-by-step execution for example of a member registration which includes some database lookup and processing, followed by a persistence of a new document of data, and to fire an activation email. You may chain up the execution in the callback method recursively, or if you don’t feel good with this style, you may chain them up with node async series function.It works well with pre-defined execution blocks. But it doesn’t make you happy in case an iteration containing a series of asynchronous execution, and you need to make sure the results arrive as same as the invoking sequence.

The code in first image fire the synchronous function (for example some disk I/O) in the sequence of the queue (simply govern by the for-loop). The mockIO function simulate a blocking process finished in random time (up to 2 seconds). 


However the result does not come back as you expect.


A recursive approach to trigger the callback function can help in this situation. We use the same mockIO function and queue. it pass the control to callback function and the result runs in the invoking sequence. 


This result is what you want!


Most people argue we should not writing blocking code in Node.js because it is single-thread process which will degrade the performance of the whole process. I agree with this point without doubt. This is a demonstration one of the way to program a series of asynchronous functions which parameter may depends on the result of the last execution. Happy coding!