function foo(message, delay) {
return new Promise(resolve => {
setTimeout(() => {
console.log(`${message} after sleeping for ${delay}ms`)
resolve()
}, delay)
})
}
// Instead of doing this:
// async function callAsync(callable, n) {
// for (let i = 0; i < n; i++) {
// await callable(`X${i}`, 5)
// }
// }
// on a non async/await supported browser, you can do this:
function callAsync(callable, n) {
return new Promise(resolve => {
let i = 0
let prom
const thenHandler = () => {
if (i === n) {
resolve()
} else {
i++
prom = foo(`X${i}`, 5).then(thenHandler())
}
}
prom = foo(`X${i}`, 5).then(() => thenHandler())
})
}
callAsync(foo, 15).then(() => console.log("done"))
To embed this program on your website, copy the following code and paste it into your website's HTML: