JSFiddle - React, Tailwind, and code Playground
JavaScript
function myRandom(message) {
return new Promise((resolve) => {
let timeout1, timeout2
timeout1 = setTimeout(() => {
resolve(`cancel ${message}`)
clearTimeout(timeout2)
}, 1000)
timeout2 = setTimeout(() => {
resolve(`complete ${message}`)
clearTimeout(timeout1)
}, Math.random() * 2000)
})
}
(async () => {
(
await Promise.all(
[1, 2, 3].map(i => myRandom(i))
)
).forEach(message => console.log(message))
})()