Skip to content

Commit

Permalink
FIx promise rejection behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrow7000 committed Jan 29, 2020
1 parent 00ccd23 commit b54e7ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 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.1",
"version": "0.10.2",
"description": "Queue asynchronous functions",
"source": "qew.ts",
"main": "dist/qew.js",
Expand Down
30 changes: 19 additions & 11 deletions qew.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
type AsyncFunc<T> = () => Promise<T>;

function makeTriggerablePromise<T>(): [Promise<T>, (inp: T) => void] {
function makeTriggerablePromise<T>(): [
Promise<T>,
(inp: T) => void,
(error: any) => void
] {
let triggerResolveWith!: (inp: T) => void;
const promToReturn: Promise<T> = new Promise(resolve => {
let triggerRejectWith!: (error: any) => void;
const promToReturn: Promise<T> = new Promise((resolve, reject) => {
const funcThatResolvesProm = (inp: T) => resolve(inp);
triggerResolveWith = funcThatResolvesProm;
triggerRejectWith = reject;
});
return [promToReturn, triggerResolveWith];
return [promToReturn, triggerResolveWith, triggerRejectWith];
}

export class Qew {
Expand All @@ -26,17 +32,19 @@ export class Qew {
* @param asyncFunc the async function to push onto this queue
*/
public push<T>(asyncFunc: AsyncFunc<T>) {
const [prom, resolveProm] = makeTriggerablePromise<T>();
const [prom, resolveProm, rejectProm] = makeTriggerablePromise<T>();

const funcToRun = () => {
asyncFunc().then(result => {
resolveProm(result);
this.executing = this.executing - 1;
asyncFunc()
.then(result => {
resolveProm(result);
this.executing = this.executing - 1;

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

this.queue = [...this.queue, funcToRun];
Expand Down

0 comments on commit b54e7ad

Please sign in to comment.