Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
function wait(ms, value) {
return new Promise((resolve) => {
setTimeout(() => resolve(value), ms);
});
}
Promise.all([wait(30, "a"), wait(10, "b")])
.then((values) => console.log(values.join(" ")));
console.log("start");start a b
- Expecting
start b a:Promise.allgives the values in the order of the input array, not in the order the promises were fulfilled. - Expecting
start b:Promise.allwaits for every promise in the array, not only the first one that is ready.
- How long does the program run before it prints the array, and why?
- What happens if
wait(10, "b")is replaced with a rejected promise?