Skip to content

Commit

Permalink
fix(kv): update until exit condition
Browse files Browse the repository at this point in the history
- related: a2edaa4
  • Loading branch information
lukeed committed May 6, 2021
1 parent 4ff4585 commit 20e6636
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,11 @@ until('should be a function', () => {
assert.type(KV.until, 'function');
});

until('should loop until first `false` value', async () => {
until('should loop until first `nullish` value', async () => {
let values = ['1', '2', '3', '4', '5'];
let output = await KV.until(
() => values.shift() || '',
(x) => Promise.resolve(+x < 3),
(x) => Promise.resolve(+x < 3 ? +x : null)
);
assert.is(output, '3');
assert.equal(values, ['4', '5']);
Expand Down
6 changes: 3 additions & 3 deletions src/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ export async function paginate<M extends KV.Metadata>(

export async function until<X extends string>(
toMake: () => X,
toSearch: (val: X) => Promise<unknown | false>
toSearch: (val: X) => Promise<unknown|false>
): Promise<X> {
let exists, tmp = '' as X;
while (exists !== false) {
while (true) {
exists = await toSearch(tmp = toMake());
if (exists == null) return tmp;
}
return tmp;
}

0 comments on commit 20e6636

Please sign in to comment.