Skip to content

Commit

Permalink
feat: add "run"
Browse files Browse the repository at this point in the history
  • Loading branch information
Krivega committed Mar 15, 2023
1 parent 2c3b9cc commit 50ef174
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('creteStackPromises', () => {
});
});

it('2 promise: sync', () => {
it('2 promise add: sync', () => {
expect.assertions(1);

stackPromises.add(() => {
Expand All @@ -69,6 +69,37 @@ describe('creteStackPromises', () => {
});
});

it('1 promise run', () => {
expect.assertions(1);

return stackPromises
.run(() => {
return delayPromise(1, 1);
})
.then((data) => {
expect(data).toBe(1);
});
});

it('2 promise run: sync', () => {
expect.assertions(2);

const promise1 = stackPromises.run(() => {
return delayPromise(1, 1);
});
const promise2 = stackPromises.run(() => {
return delayPromise(1, 2);
});

return Promise.allSettled([promise1, promise2]).then((args) => {
// @ts-ignore
const [{ reason }, { value }] = args;

expect(isPromiseIsNotActualError(reason)).toBe(true);
expect(value).toBe(2);
});
});

it('2 equal promise: sync', () => {
expect.assertions(3);

Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ const creteStackPromises = <T = any>() => {
return result;
};

const run = (task: TTask) => {
addTaskToStack(task);

return result();
};

const clearStacks = () => {
runnersStack.length = 0;
tasksStack.length = 0;
Expand All @@ -140,6 +146,7 @@ const creteStackPromises = <T = any>() => {
};

result.add = addTaskToStack;
result.run = run;
result.stop = stop;

return result;
Expand Down

0 comments on commit 50ef174

Please sign in to comment.