Vradhit
Vradhit3y ago

Vradhit – 16-20 Feb 4

How to convert async settime out to synchronous
11 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ScriptyChris
ScriptyChris3y ago
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?
Vradhit
Vradhit3y ago
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?
ScriptyChris
ScriptyChris3y ago
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
ScriptyChris
ScriptyChris3y ago
!mdn Promise.prototype.then
ScriptyChris
ScriptyChris3y ago
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
JSConf
YouTube
What the heck is the event loop anyway? | Philip Roberts | JSConf EU
JavaScript programmers like to use words like, “event-loop”, “non-blocking”, “callback”, “asynchronous”, “single-threaded” and “concurrency”. We say things like “don’t block the event loop”, “make sure your code runs at 60 frames-per-second”, “well of course, it won’t work, that function is an asynchronous callback!” If you’re anything like me...
Vradhit
Vradhit3y ago
When we use await So the output is console.log('hi') console.log ('first') Thanks u @ScriptyChris
ScriptyChris
ScriptyChris3y ago
though remember that await may only be used inside async function or at the top level of module file (or in console)
Vradhit
Vradhit3y ago
Ok thank u very much
reactibot
reactibot3y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/939193653807693844