R
Reactiflux

Vradhit – 16-20 Feb 4

Vradhit – 16-20 Feb 4

VVradhit2/4/2022
How to convert async settime out to synchronous
UUUnknown User2/4/2022
Message Not Public
Sign In & Join Server To View
SScriptyChris2/4/2022
in a dirty way you can use while (true) with date diffing to stop the loop but the question is: why do you need synchronous waiting?
VVradhit2/4/2022
No I mean there was console.log('first') after that setTimeout(console.log('hi') , 0) Question is how to make settimeout console to display first followed by first console?
SScriptyChris2/4/2022
you would like to await it
await new Promise(resolve => {
setTimeout(() => {
console.log('hi');
resolve();
}, 0)
});
console.log('first');
await new Promise(resolve => {
setTimeout(() => {
console.log('hi');
resolve();
}, 0)
});
console.log('first');
or .then() the promise instead of awaiting it !mdn await
UUUnknown User2/4/2022
Message Not Public
Sign In & Join Server To View
SScriptyChris2/4/2022
!mdn Promise.prototype.then By the way, your question indicates that you are new to event loop (or asynchrony at all) inside JS. So i'd recommend you to get familiar with it: https://www.youtube.com/watch?v=8aGhZQkoFbQ https://javascript.info/async
VVradhit2/4/2022
When we use await So the output is console.log('hi') console.log ('first') Thanks u @ScriptyChris
SScriptyChris2/4/2022
though remember that await may only be used inside async function or at the top level of module file (or in console)
VVradhit2/5/2022
Ok thank u very much
UUUnknown User2/5/2022
Message Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

Vradhit – 16-20 Feb 4

Join Server