0
Is there any way to wait for a forloop to complete (without callback)...?
I want to wait for a loop to complete... Currently I'm doing it with callback.... Is there anything to do the same thing more elegantly...?
3 Answers
+ 1
You can consider use promise with async and await
For example
function processForLoop() {
return new Promise(resolve => {
for(var i=0;i<100;i++) {
console.log(i)
}
resolve(i)
});
}
async function getForLoopOutput() {
var out = await processForLoop();
console.log("Resolve output = " + out); // get result from output of processForLoop()
}
getForLoopOutput();
https://code.sololearn.com/W5VANf0x5Dj9/?ref=app
0
Thank you bro....
But this is hand made...
Is there any dedicated logic to get the same thing?
0
What do you mean by hand make?
The function works on any delay callback as well since async is always ready to wait for the output from Promise.