Open 2zH opened 6 years ago
const sleep = t => () => new Promise(resolve => setTimeout(() => {console.log(t); resolve(t)}, t)) const example = [sleep(3000), sleep(1000), sleep(5000)] const promiseReducer = example.reduce.bind(example) // Test case 1 promiseReducer((r, f) => r.then(() => f()), Promise.resolve()) // 3000 // 1000 // 5000 // Test case 2 promiseReducer(async (r, f) => await f(), Promise.resolve()) // 1000 // 3000 // 5000 // Test case 3 promiseReducer(async (r, f) => { let result = await f; return result }, Promise.resolve()) // 1000 // 3000 // 5000 // Test case 4 promiseReducer(async (r, f) => { let a = await f();await sleep(2000)();console.log(a);return a; }, Promise.resolve()) // 1000 // 3000 // 2000 // 1000 // 5000 // 2000 // 3000 // 2000 // 5000 // Test case 5 promiseReducer(async (r, f) => { await r;let a = await f();await sleep(2000)();console.log(a);return a; }, Promise.resolve()) // 3000 // 2000 // 3000 // 1000 // 2000 // 1000 // 5000 // 2000 // 5000
Promise.all :: [Promise] -> Promise Promise.all 的执行过程保持异步。