C
h
i
L
L
u
.
.
.
Back to ArticlesAsync/Await vs Promises: A Practical GuideJavaScript

Async/Await vs Promises: A Practical Guide

avatarSombir RedhuMar 8, 20264 min read175
javascriptasyncpromiseses6

Promises were introduced in ES6 to handle async operations without callback hell. Async/await, introduced in ES2017, is syntactic sugar on top of Promises that makes async code look synchronous.

// Promise style\nfetch("/api/data")\n  .then(res => res.json())\n  .then(data => console.log(data))\n  .catch(err => console.error(err));\n\n// Async/await style\nasync function loadData() {\n  try {\n    const res = await fetch("/api/data");\n    const data = await res.json();\n    console.log(data);\n  } catch (err) {\n    console.error(err);\n  }\n}

Async/await is generally preferred for readability. But Promises shine when you need to run multiple async operations in parallel using Promise.all().

Sponsored

Comments0

?

No comments yet. Be the first!