make Prettier
This commit is contained in:
parent
038b6cac33
commit
dd206891d5
127
batchasync.js
127
batchasync.js
|
@ -1,71 +1,70 @@
|
||||||
(function (exports) {
|
(function(exports) {
|
||||||
'use strict';
|
"use strict";
|
||||||
|
|
||||||
exports.batchAsync = function (limit, arr, doStuff) {
|
exports.batchAsync = function(limit, arr, doStuff) {
|
||||||
arr = arr.slice(0);
|
arr = arr.slice(0);
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
var total = arr.length;
|
var total = arr.length;
|
||||||
var active = 0;
|
var active = 0;
|
||||||
var results = [];
|
var results = [];
|
||||||
var error;
|
var error;
|
||||||
|
|
||||||
function doMoreStuff() {
|
function doMoreStuff() {
|
||||||
// Don't take on any more tasks if we've errored,
|
// Don't take on any more tasks if we've errored,
|
||||||
// or if too many are already in progress
|
// or if too many are already in progress
|
||||||
if (error || active > limit) {
|
if (error || active > limit) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there are no more tasks to start, return
|
// If there are no more tasks to start, return
|
||||||
if (!arr.length) {
|
if (!arr.length) {
|
||||||
// If everything is also *finished*, resolve
|
// If everything is also *finished*, resolve
|
||||||
if (active < 1) {
|
if (active < 1) {
|
||||||
resolve(results);
|
resolve(results);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We need to dequeue the task here so the index is correct
|
// We need to dequeue the task here so the index is correct
|
||||||
// (keep in mind we want to support sync and async)
|
// (keep in mind we want to support sync and async)
|
||||||
var index = total - arr.length;
|
var index = total - arr.length;
|
||||||
var task = arr.shift();
|
var task = arr.shift();
|
||||||
active += 1;
|
active += 1;
|
||||||
|
|
||||||
// Spawn another task immediately,
|
// Spawn another task immediately,
|
||||||
// which will be stopped if we're at the limit
|
// which will be stopped if we're at the limit
|
||||||
doMoreStuff();
|
doMoreStuff();
|
||||||
|
|
||||||
var p;
|
var p;
|
||||||
try {
|
try {
|
||||||
p = doStuff(task);
|
p = doStuff(task);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// we need to handle, and bubble, synchronous errors
|
// we need to handle, and bubble, synchronous errors
|
||||||
error = e;
|
error = e;
|
||||||
reject(e);
|
reject(e);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
// Do stuff and then decrease the active counter when done
|
// Do stuff and then decrease the active counter when done
|
||||||
// add support for sync by rapping in a promise
|
// add support for sync by rapping in a promise
|
||||||
Promise.resolve(p)
|
Promise.resolve(p)
|
||||||
.then(function(result) {
|
.then(function(result) {
|
||||||
if ('undefined' === typeof result) {
|
if ("undefined" === typeof result) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"result was 'undefined'. Please return 'null' to signal that you didn't just forget to return another promise."
|
"result was 'undefined'. Please return 'null' to signal that you didn't just forget to return another promise."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
active -= 1;
|
active -= 1;
|
||||||
results[index] = result;
|
results[index] = result;
|
||||||
})
|
})
|
||||||
.then(doMoreStuff)
|
.then(doMoreStuff)
|
||||||
.catch(function(e) {
|
.catch(function(e) {
|
||||||
// handle async errors
|
// handle async errors
|
||||||
error = e;
|
error = e;
|
||||||
reject(e);
|
reject(e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
doMoreStuff();
|
doMoreStuff();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
})("undefined" !== typeof window ? window : module.exports);
|
||||||
}('undefined' !== typeof window ? window : module.exports));
|
|
||||||
|
|
Loading…
Reference in New Issue