Felipe Sousa

Close

Translate to Portuguese

Search Results

No results found for .

JS Async: async/await

JS Async: async/await

Published at

This post is the last of the JS Async series, with 3 posts to explain and show how to work with asynchronous data in JavaScript.

You can check out the other 2 previous articles:

Today we’ll talk about async/await and see some examples of how and what we can do with it:

Async

The keyword async was implemented in ES2017. It makes it possible to create naturally asynchronous functions using the following notation:

1 async function myAsyncFunction() {}

Something important and even more interesting about this implementation is that every async function returns a Promise, meaning that we can use all the interfaces we already know in the article about promises. Let's look at an example to better understand:

1 2 3 4 5 6 7 async function myAsyncFunction() { return 'hello!'; } myAsyncFunction().then(payload => { console.log(payload); });

The async functions use the success values as the values that will be arranged within the.then pipeline in the promise that will be returned, in case you need to export an error, it is necessary to trigger an error within the scope of execution to be sent to the .catch pipeline, let's see an example:

1 2 3 4 5 6 7 async function myAsyncFunctionWithError() { throw new Error('ops!') } myAsyncFunctionWithError().catch(error => { console.log(error); });

Await

The use of await is restricted only within a function declared with the keyword async, basically what it does is wait for the Promise response value or convert the value into a resolved Promise.

1 2 3 4 5 6 async function myAsyncFunction() { const payload = await { name: "felipe", age: 22}; console.log(payload); } myAsyncFunction();

In cases where we are not returning any value from our function, the execution call remains a normal function call without using .then.

Catching errors with try/catch

Await always expects the success value of the promise, so we have no way to capture the error directly, to do this we have to make use of the try/catch which receives the reject value if it happens, within the promises that are being executed inside the try block:

1 2 3 4 5 6 7 8 9 10 11 12 13 async function myAsyncErrorFunction(){ throw new Error("ops, something wrong happened"); } async function myAsyncFunction() { try { const response = await myAsyncErrorFunction(); } catch(error) { console.log(error); // ops, something wrong happened } } myAsyncFunction();

Executing this block, the error happens inside the promise myAsyncErrorFunction and is captured inside the try/catch catch block.

In summary, the joint use of the implementations makes our code extremely simple and readable, making handling asynchronous (or synchronous) data more direct and effective.

I hope you enjoyed this series, see you later!

🔭

Next Read

Cover Image for JS Async: Promises

JS Async: Promises

Luis Felipe Sousa
Published at

This post is the second in a series of 3 posts to explain and show how to work with asynchronous data in JavaScript.

Cover Image for JS Async: Callbacks

JS Async: Callbacks

Luis Felipe Sousa
Published at

Asynchronous patterns are part of everyday life, can be a timer, reading a file or making a request, etc., at some point, you will need to manipulate this pattern and it is very important to know how to work with them and which strategy is...