Skip to content

Commit

Permalink
Restore making delay a function + error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrow7000 committed Jan 29, 2020
1 parent b54e7ad commit 7ca99f1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qew",
"version": "0.10.2",
"version": "0.10.3",
"description": "Queue asynchronous functions",
"source": "qew.ts",
"main": "dist/qew.js",
Expand Down
18 changes: 16 additions & 2 deletions qew.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type AsyncFunc<T> = () => Promise<T>;
type DelayOrDelayer = number | (() => number);

function makeTriggerablePromise<T>(): [
Promise<T>,
Expand All @@ -25,7 +26,17 @@ export class Qew {
* @param delay how many ms to wait between when one function has resolved and
* the next one is run
*/
constructor(private maxConcurrent = 1, private delay = 0) {}
constructor(
private maxConcurrent = 1,
private delay: number | (() => number) = 0
) {
if (maxConcurrent < 1) {
throw new Error("maxConcurrent has to be 1 or higher");
}
if (typeof delay === "number" && delay < 0) {
throw new Error("delay should be a positive number");
}
}

/**
* Push another async function onto the queue
Expand All @@ -40,9 +51,12 @@ export class Qew {
resolveProm(result);
this.executing = this.executing - 1;

const delay =
typeof this.delay === "function" ? this.delay() : this.delay;

setTimeout(() => {
this.tryMove();
}, this.delay);
}, delay);
})
.catch(rejectProm);
};
Expand Down

0 comments on commit 7ca99f1

Please sign in to comment.