Skip to content

Commit

Permalink
fixed until behavior in case condition throws (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sha1n authored Jan 22, 2022
1 parent 04ff39e commit 058c9fc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions lib/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ async function until(
condition: () => boolean,
opts?: { interval?: number; timeout: number; units?: TimeUnit }
): Promise<void> {
if (condition()) {
return;
}

const defaultInterval = 50;
const deadline = opts ? Date.now() + toMilliseconds(opts.timeout, opts.units) : Number.MAX_VALUE;
const interval = opts?.interval ? toMilliseconds(opts.interval, opts.units) : defaultInterval;
Expand All @@ -66,9 +62,14 @@ async function until(
reject(new Error('Timeout'));
}

if (condition()) {
try {
if (condition()) {
clearInterval(handle);
resolve();
}
} catch (e) {
clearInterval(handle);
resolve();
reject(e);
}
}, interval);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sha1n/about-time",
"version": "0.0.4",
"version": "0.0.5",
"description": "A set of essential time related utilities",
"repository": "https://github.com/sha1n/about-time",
"author": "Shai Nagar",
Expand Down
2 changes: 1 addition & 1 deletion test/until.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe.each(cases())('%s', ({ fn }) => {
throw expectedError;
};

await expect(fn(never, { timeout: 1, units: TimeUnit.Milliseconds })).rejects.toThrow(expectedError);
await expect(fn(never, { timeout: 1, units: TimeUnit.Seconds })).rejects.toThrow(expectedError);
});

test('should resolve when the provided condition is true within a specified timeout boundary', async () => {
Expand Down

0 comments on commit 058c9fc

Please sign in to comment.