if JS is sychronous why does it not wait for set timeout to complete? code example
Example 1: javascript best way to create synchronous pause in program
console.log("Start");
console.time("Promise");
await new Promise(done => setTimeout(() => done(), 5000));
console.log("End");
console.timeEnd("Promise");
Example 2: javascript synchronous wait
function delay(n) {
n = n || 2000;
return new Promise(done => {
setTimeout(() => {
done();
}, n);
});
}
Example 3: wait until a function finishes javascript
function firstFunction(_callback){
_callback();
}
function secondFunction(){
firstFunction(function() {
console.log('huzzah, I\'m done!');
});
}