actual-server/util/async.js
2022-03-31 13:19:08 -04:00

44 lines
894 B
JavaScript

function sequential(fn) {
let sequenceState = {
running: null,
queue: []
};
function pump() {
if (sequenceState.queue.length > 0) {
const next = sequenceState.queue.shift();
run(next.args, next.resolve, next.reject);
} else {
sequenceState.running = null;
}
}
function run(args, resolve, reject) {
sequenceState.running = fn(...args);
sequenceState.running.then(
val => {
pump();
resolve(val);
},
err => {
pump();
reject(err);
}
);
}
return (...args) => {
if (!sequenceState.running) {
return new Promise((resolve, reject) => {
return run(args, resolve, reject);
});
} else {
return new Promise((resolve, reject) => {
sequenceState.queue.push({ resolve, reject, args });
});
}
};
}
module.exports = { sequential };