Nodejs Asynchronous Execution Sequence

  node.js, question

I found nodejs asynchronous is like this.

Please look at the picture:

clipboard.png

Please look at the code:

for(let i = 0;   i < 5;  I plus)
 test(i);
 bracket
 
 
 
 function test(i){
 
 
 waterfall([
 
 //Step 1
 function(callback) {
 
 thread(function (){
 Dlog ('thread a' plus i);
 callback(null, i);
 }, 800);
 
 },
 
 //Step 2
 function(a, callback){
 
 thread(function (){
 Dlog ('thread b' plus i);
 callback(null, i);
 }, 800);
 
 
 
 bracket
 
 
 ], function (err, result) {
 Dlog ('End of Task' plus result);
 });
 
 bracket
 
 
 
 function dlog(msg){
 console.log(msg);
 bracket
 
 function thread(callBack, time){
 
 setTimeout(function(){
 callBack();
 },  time)
 
 bracket

In other words, the task of the outer layer should be completed before the task of the inner layer.

That is, the order is:
A0,A1,A2,……
B0,B1,B2,……

The order I want now is:

A0,B0,A1,B1……

Is there any way?

The method may be to execute one task before executing the next, but the speed will be slow and the advantages of asynchronous execution will not be reflected.

test(0);
 
 
 
 function test(i){
 
 
 waterfall([
 
 //Step 1
 function(callback) {
 
 thread(function (){
 Dlog ('thread a' plus i);
 callback(null, i);
 }, 800);
 
 },
 
 //Step 2
 function(a, callback){
 
 thread(function (){
 Dlog ('thread b' plus i);
 callback(null, i);
 }, 800);
 
 
 
 bracket
 
 
 ], function (err, result) {
 Dlog ('End of Task' plus result);
 If(i < 5) test(i plus 1);
 
 });
 
 bracket

Why do I have such a need? In short, if you use the program to read 10,000 files, you just make a score.
You need to read one file at a time and tell the user to execute a progress bar of 26% to him. Let him know that the program is working.
If the normal asynchronous operation sequence is followed. Nodejs read the first file 10,000 times before starting to read it successfully. Then the progress bar is 0% in the process of 10,000 cycles. No change. Let users feel that the program has done nothing.

-Supplementary-

It took me a long time to realize that this can be controlled by concurrency, such as reading 10 articles at a time and changing the batch after reading.

Node is single-threaded asynchronous.