Home > OS >  What does Subscription add function do with Promise's resolve call-back
What does Subscription add function do with Promise's resolve call-back

Time:07-17

I can see in GitHub project angular-10-signup-verification-boilerplate the following snippet:

export function appInitializer(accountService: AccountService) {
    return () => new Promise(resolve => {
        // attempt to refresh token on app start up to auto authenticate
        accountService.refreshToken()
            .subscribe()
            .add(resolve);
    });
}

I was wondering if somebody could explain what is the meaning of add function with parameter resolve. Documentation says that add function is for adding additional subscribers so they can be unsubscribed in one go.

CodePudding user response:

The add method of the subscription returned from subscribe will run when the subscription completes (and is subsequently unsubscribed)

In your example the resolve will be called when the refreshToken completes. When the Promise resolves and the APP_INITIALIZER (which is waiting for the promise to resolve) will continue

Note: I've never seen it used before - the more common approach is to use the subscribe next/error/complete functions or a finalize operator

  • Related