According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all, two evaluations in the following code runs asynchronously.
var p2 = Promise.all([1337, "hi"]); // non-promise values will be ignored, but the evaluation will be done asynchronously
and a question occurs to me that, in the following example:
async function sleep(x) {
await new Promise((resolve) => setTimeout(resolve, x))
}
async function test(x) {
await sleep(1000);
return x
}
async function run(){
let promises = []
promises.push(test(1))
promises.push(test(2))
let res = await Promise.race(promises)
console.log(res)
}
run()
// I ran it for several times, and it always give me 1
Since these two evaluation ran asynchronously and they consume the same amount of time, is it possible for this program to give me 2 in some cases?
What I dont understand is:
- Why this program always gives me 1? If multiple evaluations consume the same amount of time, does javascript promise the earlier one always finish earlier?
- Does thread context switching happens in Promise.all()? I've tried another example code of multithreading, and it always gives me 100.
let a = 0
async function test1() {
a ;
}
async function run(){
let promises = []
for (let i=0;i<100;i ){
promises.push(test1())
}
await Promise.all(promises)
console.log(a)
}
run()
// It always give me 100
It seems to never violate atomicity, why?
CodePudding user response:
No. How Event loop in javascript works is, it will put two timeout callbacks in a "timeout queue" and because the both timeouts have the same timeout delay (1000) their callbacks will be executed in the order of their appearance in the "timeout queue". First "1", then "2".
CodePudding user response:
Why this program always gives me 1? If multiple evaluations consume the same amount of time, does javascript promise the earlier one always finish earlier?
The other answer answered this – the timeouts get resolved in the order they're put into the queue. The timeout deadline for the 2
promise is always necessarily later than the one for 1
promise anyway, since some time will have passed between promises.push(test(1))
and promises.push(test(2))
.
The second question:
Does thread context switching happens in Promise.all()? I've tried another example code of multithreading, and it always gives me 100.
No, you didn't try multithreading because there is no multithreading in regular JavaScript.
An async
function is synchronous until the first await
in it (and sugars its return value to be wrapped in a promise), so
async function test1() {
a ;
// implicit return undefined;
}
is equivalent to
function test1() {
a ;
return Promise.resolve(undefined);
}
This means that
async function run() {
let promises = []
for (let i = 0; i < 100; i ) {
promises.push(test1())
}
await Promise.all(promises)
console.log(a)
}
is equivalent to
async function run() {
let promises = []
for (let i = 0; i < 100; i ) {
a ;
promises.push(Promise.resolve(undefined));
}
await Promise.all(promises);
console.log(a);
}
which is plainly synchronous in how it modifies a
.