Skip to content

Commit

Permalink
fix asynchronously handled promises; restore test which was broken by…
Browse files Browse the repository at this point in the history
… that
  • Loading branch information
autopulated committed Jan 30, 2024
1 parent c553c52 commit 99714f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,16 @@ class BaseModel {
// returns an array of models (possibly empty)
const rawQuery = BaseModel.#convertQuery(this, query, Object.assign({startAfter: otherOptions.startAfter, limit: otherOptions.limmit}, rawQueryOptions));
const pending = [];
let errorOccurred = false;
const setErrorOccurred = () => { errorOccurred = true; };
// ... relying on #rawQueryIdsBatchIterator to return the right number in total, based on otherOptions.limit
for await (const ids of BaseModel.#rawQueryIdsBatchIterator(this, rawQuery, otherOptions)) {
if (errorOccurred) break;
// start fetching the models from the IDs of this batch immediately, but don't await yet so requests can be parallelised
pending.push(BaseModel.#getByIds(this, ids, Object.assign({abortSignal: otherOptions.abortSignal}, rawFetchOptions)));
const pendingGetByIds = BaseModel.#getByIds(this, ids, Object.assign({abortSignal: otherOptions.abortSignal}, rawFetchOptions));
pending.push(pendingGetByIds);
// however, we must .catch any errors, so we can fail fast (and prevent PromiseRejectionHandledWarning logs)
pendingGetByIds.catch(setErrorOccurred);
}
return (await Promise.all(pending)).flat();
}
Expand Down
9 changes: 4 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1533,11 +1533,10 @@ t.test('queries:', async t => {
t.end();
});

// this test is failing because it uses asynchronously handled promise rejections (and breaks the rest of the test suite as well)
// await t.test('querying via a model of the wrong type', async t => {
// t.rejects(Bar.queryMany({type:'namespace.foo'}), {message:'Document does not match schema for ambiguous.bar. The loaded document has a different type "namespace.foo", and the schema is incompatible: must have required property \'barVal\'.'}, 'should throw if the query returns a document of an incompatible type');
// t.end();
// });
await t.test('querying via a model of the wrong type', async t => {
t.rejects(Bar.queryMany({type:'namespace.foo'}), {message:'Document does not match schema for ambiguous.bar. The loaded document has a different type "namespace.foo", and the schema is incompatible: must have required property \'barVal\'.'}, 'should throw if the query returns a document of an incompatible type');
t.end();
});

t.end();
});
Expand Down

0 comments on commit 99714f0

Please sign in to comment.