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?

7 Answers
7

Leave a Comment