Reputation: 14468
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?
Upvotes: 2
Views: 4990
Reputation: 501
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;
Upvotes: 2
Reputation: 3526
Yes if there is an error not catched inside the getAccessToken, the value inside the promise will be null
const accessToken = await getAccessToken().catch(() => null); //accessToken will be a promise
accessToken .then(function(value) {
console.log(value); // expected output: null
});
Upvotes: 0
Reputation: 1019
"The catch()
method returns a Promise and deals with rejected cases only." Source
So that line simply says that if an error occures while completing the promise, () => null
, which is basically return null inside the Promise.
Upvotes: 1