Junior — Senior
Organization of sequential calls of asynchronous functions
livecode
Task condition
You need to call the functions so that the numbers 1 2 3 are displayed sequentially in the console.
const getRandomArbitrary = (min, max) =>
Math.floor(Math.random() * (max - min) + min);
const first = () => {
return new Promise((resolve) => {
setTimeout(() => console.log("1"), getRandomArbitrary(0, 3) * 1000);
});
};
const second = () => {
return new Promise((resolve) => {
setTimeout(() => console.log("2"), getRandomArbitrary(0, 3) * 1000);
});
};
const third = () => {
new Promise((resolve) => {
setTimeout(() => console.log("3"), getRandomArbitrary(0, 3) * 1000);
});
};
Consider which approaches (callback chains, async/await, Promise composition) will guarantee the output order regardless of random delays.