What is the correct way to call the same function on an array of objects using callbacks to advance?
Essentially processing asynchronous calls sequentially.
doAsynchFunction(data,callback){
console.log(data);
wait(1000,callback); // do something that takes some time and execute callback
}
var a=[1,2,3,4,5,6];
I'd like to see the numbers appear 1 second apart
You could use
Promise.all()
to process asynchronous processes where the result could be returned in any orderor use a queue to process asynchronous functions in sequential order
Adjusting
js
at updated Question to utilizingsetTimeout()
,Function.prototype.bind()
to pass function reference with parameters tosetTimeout
. Note,callback
in this instance would bedoAsynchFunction
itself.this is what I'm using right now