Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(CancellablePromise): allSettled return type #15

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/CancellablePromise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,36 @@ describe('all', function () {
expect(promise1.cancel).toHaveBeenCalled();
expect(promise2.cancel).toHaveBeenCalled();
});
test('should reject with an error when the `all` method is used with some rejected promises', async () => {
expect.assertions(1);

const promises = [
new CancellablePromise(Promise.resolve('some value')),
new CancellablePromise(Promise.reject('first reason')),
new CancellablePromise(Promise.reject('second reason')),
];

try {
await CancellablePromise.all(promises);
} catch (error) {
expect(error).toBe('first reason');
}
});
test('should resolve with some settled result when the `allSettled` method is used with some rejected promises', async () => {
expect.assertions(1);

const promises = [
new CancellablePromise(Promise.resolve('some value')),
new CancellablePromise(Promise.reject('some reason')),
];

try {
const result = await CancellablePromise.allSettled(promises);

expect(result).toStrictEqual([
{status: 'fulfilled', value: 'some value'},
{status: 'rejected', reason: 'some reason'},
]);
} catch (error) {}
});
});
2 changes: 1 addition & 1 deletion lib/CancellablePromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class CancellablePromise<T = unknown> implements Promise<T> {

return new CancellablePromise(
Promise.allSettled(values) as unknown as Promise<{
-readonly [P in keyof Values]: Awaited<Values[P]>;
-readonly [P in keyof Values]: PromiseSettledResult<Awaited<Values[P]>>;
}>,
cancel,
);
Expand Down
Loading