update: better parallel
This commit is contained in:
parent
ed0a398a63
commit
2db3b98a85
|
@ -1,15 +1,17 @@
|
|||
// Параллельные вычисления
|
||||
|
||||
const WAIT_CHECK_TIME = 10;
|
||||
|
||||
function parallel(promises, callback) {
|
||||
const result = [];
|
||||
|
||||
|
||||
let done = 0;
|
||||
const setResult = (i, res) => {
|
||||
done++;
|
||||
result[i] = res;
|
||||
if (done === promises.length) {
|
||||
callback(result);
|
||||
}
|
||||
}
|
||||
|
||||
for (let i in promises) {
|
||||
const promise = promises[i];
|
||||
const res = promise((res) => setResult(i, res));
|
||||
|
@ -17,20 +19,6 @@ function parallel(promises, callback) {
|
|||
setResult(i, res);
|
||||
}
|
||||
}
|
||||
|
||||
// if all sync
|
||||
if (done === promises.length) {
|
||||
callback(result);
|
||||
return;
|
||||
}
|
||||
|
||||
// if at least one is async
|
||||
const waitInterval = setInterval(() => {
|
||||
if (done === promises.length) {
|
||||
clearInterval(waitInterval);
|
||||
callback(result);
|
||||
}
|
||||
}, WAIT_CHECK_TIME);
|
||||
}
|
||||
|
||||
parallel([
|
||||
|
|
Loading…
Reference in New Issue
Block a user