We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
const timeout = (ms) => new Promise((resolve, reject) => { setTimeout(() => { resolve(); }, ms); }); const ajax1 = () => timeout(2000).then(() => { console.log("1"); return 1; }); const ajax2 = () => timeout(1000).then(() => { console.log("2"); return 2; }); const ajax3 = () => timeout(2000).then(() => { console.log("3"); return 3; }); const mergePromise = (ajaxArray) => { // 1,2,3 done [1,2,3] 此处写代码 请写出ES6、ES3 2中解法 }; mergePromise([ajax1, ajax2, ajax3]).then((data) => { console.log("done"); console.log(data); // data 为[1,2,3] }); // 执行结果为:1 2 3 done [1,2,3]
The text was updated successfully, but these errors were encountered:
每日一题会在下午四点在交流群集中讨论,五点 Github、交流群同步更新答案
Sorry, something went wrong.
const timeout = (ms) => new Promise((resolve, reject) => { setTimeout(() => { resolve() }, ms) }) const ajax1 = () => timeout(2000).then(() => { console.log('1') return 1 }) const ajax2 = () => timeout(1000).then(() => { console.log('2') return 2 }) const ajax3 = () => timeout(2000).then(() => { console.log('3') return 3 }) const mergePromise = (ajaxArray) => { var data = [] var sequence = Promise.resolve() ajaxArray.forEach((item) => { console.log('item', item) sequence = sequence.then(item).then((res) => { data.push(res) return data }) }) return sequence } mergePromise([ajax1, ajax2, ajax3]).then((data) => { console.log('done') // data 为[1,2,3] console.log(data) }) // 执行结果为:1 2 3 done [1,2,3]
const timeout = (ms) => new Promise((resolve, reject) => { setTimeout(() => { resolve() }, ms) }) const ajax1 = () => timeout(2000).then(() => { console.log(1) return 1 }) const ajax2 = () => timeout(1000).then(() => { console.log(2) return 2 }) const ajax3 = () => timeout(2000).then(() => { console.log(3) return 3 }) const mergePromise = (ajaxArray) => { return new Promise((resolve, reject) => { if (!ajaxArray.length) return resolve([]) const result = [] const request = async () => { const ajax = ajaxArray.shift() const res = await ajax() result.push(res) if (!ajaxArray.length) { resolve(result) } else { request() } } request() }) } mergePromise([ajax1, ajax2, ajax3]).then((data) => { console.log('done') console.log(data) // data 为[1,2,3] }) // 执行结果为:1 2 3 done [1,2,3]
No branches or pull requests
The text was updated successfully, but these errors were encountered: