mirror of
https://github.com/oven-sh/bun
synced 2026-02-11 03:18:53 +00:00
37 lines
763 B
TypeScript
37 lines
763 B
TypeScript
/**
|
|
* https://www.npmjs.com/package/after
|
|
*
|
|
* I refuse to install a 30 line function via npm
|
|
*/
|
|
module.exports = after;
|
|
|
|
/**
|
|
* Invoke a callback after `n` calls
|
|
*/
|
|
function after(count, callback, err_cb) {
|
|
var bail = false;
|
|
err_cb = err_cb || noop;
|
|
proxy.count = count;
|
|
|
|
return count === 0 ? callback() : proxy;
|
|
|
|
function proxy(err, result) {
|
|
if (proxy.count <= 0) {
|
|
throw new Error("after called too many times");
|
|
}
|
|
--proxy.count;
|
|
|
|
// after first error, rest are passed to err_cb
|
|
if (err) {
|
|
bail = true;
|
|
callback(err);
|
|
// future error callbacks will go to error handler
|
|
callback = err_cb;
|
|
} else if (proxy.count === 0 && !bail) {
|
|
callback(null, result);
|
|
}
|
|
}
|
|
}
|
|
|
|
function noop() {}
|