Skip to content

Commit

Permalink
Add .lazy to prevent unhandled exceptions for lazy promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Brain committed Sep 24, 2021
1 parent 9a69f2a commit 16f77dd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ export class ZalgoPromise<R : mixed> {
return Promise.resolve(this); // eslint-disable-line compat/compat
}

lazy() : ZalgoPromise<R> {
this.errorHandled = true;
return this;
}

static resolve<X, Y>(value : ZalgoPromise<X> | Y) : ZalgoPromise<X | Y> {

if (value instanceof ZalgoPromise) {
Expand Down
22 changes: 22 additions & 0 deletions test/tests/reject.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,26 @@ describe('reject cases', () => {
}, 100);
}, 100);
});

it('should not call unhandled promise method when lazy promise is rejected without having a handler', () => {

window.addEventListener('error', () => {
// pass
});

let error;

const listener = ZalgoPromise.onPossiblyUnhandledException(err => {
error = err;
});

ZalgoPromise.reject(new Error('foobar')).lazy();

return ZalgoPromise.delay(50).then(() => {
listener.cancel();
if (error) {
throw new Error(`Expected error to not be thrown`);
}
});
});
});

0 comments on commit 16f77dd

Please sign in to comment.