Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reject in async/await syntax?

How can I reject a promise that returned by an async/await function?

e.g. Originally:

foo(id: string): Promise<A> {   return new Promise((resolve, reject) => {     someAsyncPromise().then((value)=>resolve(200)).catch((err)=>reject(400))   }); } 

Translate into async/await:

async foo(id: string): Promise<A> {   try{     await someAsyncPromise();     return 200;   } catch(error) {//here goes if someAsyncPromise() rejected}     return 400; //this will result in a resolved promise.   }); } 

So, how could I properly reject this promise in this case?

like image 420
Phoenix Avatar asked Feb 25 '17 08:02

Phoenix


People also ask

How do you handle promise reject In await?

If the Promise is rejected, the await expression throws the rejected value. If the value of the expression following the await operator is not a Promise , it's converted to a resolved Promise. An await splits execution flow, allowing the caller of the async function to resume execution.

What is async-await syntax?

An async function is a function declared with the async keyword, and the await keyword is permitted within it. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.

How do you get a reject promise?

catch " around the executor automatically catches the error and turns it into rejected promise. This happens not only in the executor function, but in its handlers as well. If we throw inside a . then handler, that means a rejected promise, so the control jumps to the nearest error handler.

Does await need to return a promise?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.


1 Answers

Your best bet is to throw an Error wrapping the value, which results in a rejected promise with an Error wrapping the value:

} catch (error) {     throw new Error(400); } 

You can also just throw the value, but then there's no stack trace information:

} catch (error) {     throw 400; } 

Alternately, return a rejected promise with an Error wrapping the value, but it's not idiomatic:

} catch (error) {     return Promise.reject(new Error(400)); } 

(Or just return Promise.reject(400);, but again, then there's no context information.)

In your case, as you're using TypeScript and foo's return value is Promise<A>, you'd use this:

return Promise.reject<A>(400 /*or Error*/ ); 

In an async/await situation, that last is probably a bit of a semantic mis-match, but it does work.

If you throw an Error, that plays well with anything consuming your foo's result with await syntax:

try {     await foo(); } catch (error) {     // Here, `error` would be an `Error` (with stack trace, etc.).     // Whereas if you used `throw 400`, it would just be `400`. } 
like image 69
T.J. Crowder Avatar answered Sep 27 '22 01:09

T.J. Crowder