Is there any way to wait for a forloop to complete (without callback)...? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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...?

17th Feb 2019, 1:21 PM
Mohammad Tahsin
Mohammad Tahsin - avatar
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
17th Feb 2019, 1:58 PM
Calviղ
Calviղ - avatar
0
Thank you bro.... But this is hand made... Is there any dedicated logic to get the same thing?
17th Feb 2019, 2:07 PM
Mohammad Tahsin
Mohammad Tahsin - avatar
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.
17th Feb 2019, 2:22 PM
Calviղ
Calviղ - avatar