Skip to content

Commit

Permalink
fix(fix tryuntilasync): fixes tryUntilAsync issue where scalar was sk…
Browse files Browse the repository at this point in the history
…ipping correct behavior
  • Loading branch information
Marviel committed Nov 7, 2023
1 parent 5750ea6 commit 6b0a563
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lukebechtel/lab-ts-utils",
"version": "6.1.0",
"version": "6.1.1",
"description": "A library of small, self-contained utility functions for use in TypeScript projects.",
"main": "./lib/src/index.js",
"types": "./lib/src/index.d.ts",
Expand Down
24 changes: 22 additions & 2 deletions src/functions/tryUntilAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,32 @@ export function tryUntilAsync<TReturn>(
})
// console.log('expBackoff done')
continue;
} else if (!delay.type && delay.ms) {
} else if (delay.type === 'old' || delay.type === undefined || delay.type === null) {
if (delay.ms) {
await new Promise(delayRes => {
delayTimeout = setTimeout(delayRes, delay.ms);
});
continue;
}
else if (delay.delayFunction) {
// console.log('custom start')
await delay.delayFunction({
numFailedAttempts: attempts,
tryLimits,
err: pastErrors.getLast(),
pastErrors: pastErrors.toArray(),
});
// console.log('custom done')
continue;
}
}
else if (delay.type === 'scalar') {
await new Promise(delayRes => {
delayTimeout = setTimeout(delayRes, delay.ms);
});
continue;
} else if ((delay.type === undefined || delay.type === 'custom') && delay.delayFunction) {
}
else if (delay.type === 'custom') {
// console.log('custom start')
await delay.delayFunction({
numFailedAttempts: attempts,
Expand Down
9 changes: 6 additions & 3 deletions test/tryUntil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe('tryUntilAsync', () => {

test('scalar delay', async () => {
let attempts = 0;
const res = await tryUntilAsync({
await expect(tryUntilAsync({
func: async ({ numPreviousTries }) => {
attempts += 1;
if (numPreviousTries < 3) {
Expand All @@ -205,8 +205,11 @@ describe('tryUntilAsync', () => {
type: 'scalar',
ms: 1000,
},
});
expect(attempts).toBe(4);
tryLimits: {
maxTimeMS: 1500,
}
})).rejects.toThrow();
expect(attempts).toBe(2);
});

test('expBackoff delay', async () => {
Expand Down

0 comments on commit 6b0a563

Please sign in to comment.