任意promise串行执行算法 - 童彪
// 任意promise串行执行算法 - 童彪
function runAllPromise() { var p1 = new Promise((resove, reject) => { setTimeout(() => { resove('1') }, 2000) }) var p2 = new Promise((resove, reject) => { resove('2') }) var p3 = new Promise((resove, reject) => { resove('3') }) var p4 = new Promise((resove, reject) => { resove('4') }) var p5 = new Promise((resove, reject) => { resove('5') }) var p6 = new Promise((resove, reject) => { setTimeout(() => { resove('6') }, 0) }) let arr = [p1, p2, p3, p4, p5, p6] function runPromise(fns) { if (fns.length <= 1) { return fns[0].then(data => { console.log(data) }) } // let itemFn = fns.splice(fns.length - 1, 1); let itemFn = fns.splice(0, 1) return itemFn[0].then((data) => { console.log(data) runPromise(fns) }) } runPromise(arr) } runAllPromise()
https://www.tongbiao.xyz/