Skip to content
Open
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
7 changes: 6 additions & 1 deletion packages/core/src/pending_tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ export class PendingTasks {
*/
run(fn: () => Promise<unknown>): void {
const removeTask = this.add();
fn().catch(this.errorHandler).finally(removeTask);
try {
fn().catch(this.errorHandler).finally(removeTask);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Darn, I think we're just a month or two short of being able to use Promise.try: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/try

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I think I misremembered 1.5 years instead of 2.5 years.

Didn't know they updated that site with an expected widely available date, that's useful!

} catch (err) {
this.errorHandler(err);
removeTask();
}
}

/** @nocollapse */
Expand Down
18 changes: 18 additions & 0 deletions packages/core/test/acceptance/pending_tasks_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ describe('public PendingTasks', () => {
await expectAsync(appRef.whenStable()).toBeResolved();
expect(spy).toHaveBeenCalled();
});

it('should stop blocking stability if run function throws synchronously', async () => {
TestBed.configureTestingModule({
rethrowApplicationErrors: false,
});

const appRef = TestBed.inject(ApplicationRef);
const pendingTasks = TestBed.inject(PendingTasks);
const errorHandler = TestBed.inject(ErrorHandler);
const spy = spyOn(errorHandler, 'handleError');

pendingTasks.run(() => {
throw new Error('Sync error');
});
await expectAsync(appRef.whenStable()).toBeResolved();
expect(spy).toHaveBeenCalled();
expect(spy.calls.mostRecent().args[0].message).toContain('Sync error');
});
});

function applicationRefIsStable(applicationRef: ApplicationRef) {
Expand Down
Loading