async await
is here
(ES7), so you can do this kind of things very easily now.
var i;
var j = 10;
for (i = 0; i < j; i++) {
await asycronouseProcess();
alert(i);
}
Remember, this works only if asycronouseProcess
is returning a Promise
If asycronouseProcess
is not in your control then you can make it return a Promise
by yourself like this
function asyncProcess() {
return new Promise((resolve, reject) => {
asycronouseProcess(()=>{
resolve();
})
})
}
Then replace this line await asycronouseProcess();
by await asyncProcess();
Understanding Promises
before even looking into async await
is must
(Also read about support for async await
)