So javascript is single-threaded. Then why is the output as such? This is super simple but why is it like this? I've watched this, again the whole topic does not address this question.
function test() {
return new Promise((resolve, reject) => {
console.log('promise started');
const rand = Math.floor(Math.random() * 2);
if (rand===0){
resolve();
}
else {
reject();
}
})
}
console.log('program start');
test()
.then(() => {console.log('test ran ok')})
.catch(err => {console.log('FAILED')});
console.log('program end');
Program output:
program start
promise started
program end
test ran ok