JavaScript, async and await keywords


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

As you should already know, a promise can be rejected. So you should ask yourself: How do we deal with a rejected promise when using async and await ? If you remember the playground on promises, you learned this:

You can use the promise's catch like a normal catch. And everything is simple to understand. Keep in mind that a then callback can crash. It can throw an error (with an explicit throw or by trying to access a property of a null variable). The catch method will also catch these crashes. Repeat to yourself: the promise's catch method is like a normal catch.

So, what is the most natural way to handle an error when using async and await ? Yes, you have it. Use a simple try { ... } catch (error) { ... }. Look at the following example:

Error handling example
function job() {
return new Promise(function(resolve, reject) {
setTimeout(reject, 500, 'Error happened');
async function test() {
try {
let message = await job();
return 'Hello world';
} catch (error) {
return 'Error happened during test';
test().then(function(message) {

If you want to return a rejected promise in an async function, you just have to throw an error. Look at this example:

Return a rejected promise
async function job() {
throw new Error("Access denied");
.then(function(message) {
.catch(function(error) {
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content