Reputation:
does a promise wait until the catch is over or not? So in this example, is the "1 or 2" first shown or is "first or second" shown first?
await promise().catch(()=>{
await anotherPromise();
alert("1 or 2");
});
alert("first or second");
Upvotes: 0
Views: 538
Reputation: 664538
Yes, await
awaits the promise, and .catch(…)
returns a promise that resolves with the result of the callback (which in your case is a promise).
However, you forgot to make the callback function async
, using await
in there would throw a syntax error. And you wouldn't write it like this anyway - rather use
try {
await promise();
} catch {
await anotherPromise();
alert("first");
}
alert("second");
which also makes clear in which order the alerts can happen.
Upvotes: 1
Reputation: 138267
As by operator predescendence, nearly nothing can beat the function call and the property access operator. So independently of what you do there:
await a.b.c().d;
It will be first evalued, then the result is awaited. So it doesnt await the result of
promise()
but rather the result of
promise.catch(..)
which will return a new promise. So your code is equal to:
promise().catch(()=>{
await anotherPromise();
alert("1 or 2");
}).then(()=> alert("first or second") );
Upvotes: 1