JavaScript execute asynchronous functions in Parallel with count and Promise All In One
JavaScript 使用 count 和 Promise
並行
執行異步函數
errors
function promiseAll<T>(functions: Fn<T>[]): Promise<T[]> {
return new Promise((resolve, reject) => {
let temp = [];
for(let [i, func] of Object.entries(functions)) {
console.log(`✅ i =`, i);
console.log(`✅ func =`, func);
(async (index) => {
await func().then(value => {
temp[index] = value;
console.log(`❓ index =`, index);
console.log(`value =`, value);
console.log(`temp =`, temp);
if(temp.length === functions.length) {
return resolve(temp);
}
// temp = (3) [4, empty, 16] ❌
// if(temp.length === functions.length && !temp.some(v => Object.prototype.toString.call(v) === '[object Promise]')) {
// return resolve(temp);
// }
}).catch(err => {
return reject(err);
});
})(i);
}
});
};
function promiseAll<T>(functions: Fn<T>[]): Promise<T[]> {
return new Promise((resolve, reject) => {
let temp = [];
let index = 0;
for(let [i, func] of Object.entries(functions)) {
console.log(`✅ i =`, i);
// console.log(`func =`, func);
func().then(value => {
// temp[i] = value;
temp[index] = value;
index += 1;
// temp.push(value);
console.log(`❓ i =`, i);
// index 0 twice bug ❌ 覆蓋 bug
// 💩 temp = [ 4, <1 empty item>, 16 ]
console.log(`value =`, value);
console.log(`temp =`, temp);
// value = 16
// temp = [ 4, <1 empty item>, 16 ] 2
// setTimeout 時間先後順序 bug ❌
if(temp.length === functions.length && !temp.some(v => Object.prototype.toString.call(v) === '[object Promise]')) {
return resolve(temp);
}
// setTimeout 時間先後順序 bug ❌
// setTimeout(() => {
// if(temp.length === functions.length) {
// return resolve(temp);
// }
// }, 0);
}).then(() => {
// setTimeout 時間先後順序 bug ❌
// console.log(`temp =`, temp);
// if(temp.length === functions.length) {
// return resolve(temp);
// }
}).catch(err => {
return reject(err);
}).finally(() => {
// setTimeout 時間先後順序 bug ❌
// console.log(`temp =`, temp);
// if(temp.length === functions.length) {
// return resolve(temp);
// }
});
// if(temp.length === functions.length) {
// return resolve(temp);
// }
// let id = setInterval(() => {
// if(temp.length === functions.length && !temp.some(v => Object.prototype.toString.call(v) === '[object Promise]')) {
// clearInterval(id);
// return resolve(temp);
// }
// }, 0);
}
});
};
function promiseAll() {
return new Promise((resolve, reject) => {
let temp = [];
for(let [i, func] of Object.entries(functions)) {
// console.log(`i =`, i);
// console.log(`func =`, func);
func().then(value => {
console.log(`value =`, value);
temp[i] = value;
console.log(`temp =`, temp, i);
}).catch(err => {
return reject(err);
});
}
// ❌ bug
console.log(`temp ? =`, temp);
setTimeout(() => {
console.log(`temp =`, temp);
if(temp.length === functions.length) {
return resolve(temp);
}
}, 0);
});
};
solutions
type Fn<T> = () => Promise<T>
function promiseAll<T>(functions: Fn<T>[]): Promise<T[]> {
return new Promise((resolve, reject) => {
let temp = [];
let count = 0;
for(let [i, func] of Object.entries(functions)) {
func().then(value => {
temp[i] = value;
count += 1;
// count ✅
// 使用 count 避免出現 empty 返回值情況 ✅
if(count === functions.length) {
return resolve(temp);
}
}).catch(err => reject(err));
}
});
};
demos
LeetCode 2721. Execute Asynchronous Functions in Parallel
function promiseAll() {
return new Promise((resolve, reject) => {
let temp = [];
let count = 0;
for(let [i, func] of Object.entries(functions)) {
func().then(value => {
temp[i] = value;
count += 1;
// count ✅
// 使用 count 避免出現 empty 返回值情況 ✅
if(count === functions.length) {
return resolve(temp);
}
}).catch(err => reject(err));
}
});
};
const functions = [() => new Promise(resolve => setTimeout(() => resolve(4), 50)), () => new Promise(resolve => setTimeout(() => resolve(10), 150)), () => new Promise(resolve => setTimeout(() => resolve(16), 100))];
promiseAll(functions).then(console.log);
(🐞 反爬蟲測試!打擊盜版⚠️)如果你看到這個信息, 說明這是一篇剽竊的文章,請訪問 https://www.cnblogs.com/xgqfrms/ 查看原創文章!
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 發佈文章使用:只允許註冊用戶纔可以訪問!
原創文章,版權所有©️xgqfrms, 禁止轉載 🈲️,侵權必究⚠️!