Back to all questions

What is the difference between async/await and chaining?

Devs Hive

Devs Hive

Seen on interview: 2 users

This question often comes up when discussing Promises or the Event Loop. In short, there is no difference from a technical standpoint. You could say that async/await is syntactic sugar on top of chaining that lets you write asynchronous code in a synchronous style, as is common in most programming languages, for example C#.

Choosing between chaining and async/await is a matter of taste, but in practice the synchronous style is much easier to read, and most developers usually go with async/await.

Let's look at an example

// Використання chaining
fetchData()
.then(result => console.log(result))
.catch(error => console.error(error));

// Той самий код, але в async/await стилі
async function run() {
try {
const result = await fetchData();

console.log(final);
} catch (error) {
console.error(error);
}
}

Seen on interview?

Comments (0)

Sign in to leave a comment

No comments yet. Be the first!