Search code examples
javascriptpromiseasync-awaites6-promise

Javascript Promise, returning null from catch?


In a PR I was reviewing recently, I saw this:

const accessToken = await getAccessToken().catch(() => null);

My question is: Is the catch doing anything here? Does it assign null to accessToken? Would it be different without it?


Solution

  • Arrow functions have implicit returns.

    In your code snippet, if getAccessToken() is rejected, catch() will be entered, null will be the value which gets returned to accessToken.

    In essence acccessToken could equal (depending on the outcome):

    (an example access token)

    accessToken = asda8sdaewrascsac;

    OR

    accessToken = null;