promise 递归

let counter = 0;
const max = 5;
function promise () {
console.log(waiting ${counter ++}s...);
return new Promise(resolve => {
if (counter >= max) {
return resolve(“promise finish”);
} else {
setTimeout(() => {
promise().then(res => resolve(res));
}, 1000);
}
})}

promise()
.then(res => {
console.log(res)
})

你可能感兴趣的:(javascript)