I have two async
functions. Both of them are waiting for two 3 seconds function calls. But the second one is faster than the first. I think the faster one is runnin
Because thesleep()
function is a synchronous function, it just return a asynchronous promise, eg:
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
In parallel()
, the two sleep()
synchronously init two promise,they wait to be resolved meanwhile, it's going to be about 3s.
However, in serial()
, the two await sleep()
means that the second sleep()
promise must wait for the first sleep()
to be resolved, so it's going to be about 6s.