Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

luiz tanure
luiz tanure

Posted on • Originally published atletanure.dev

Async/Await – Simplifying Asynchronous JavaScript

Note: This article was originally published on October 10, 2017. Some information may be outdated.

async andawait landed in ES2017, letting developers write asynchronous code that reads like synchronous steps.

Before: Promise chain

functionfetchUser(id){returnfetch(`/api/users/${id}`).then(res=>res.json()).then(data=>{console.log('User:',data);}).catch(err=>console.error(err));}
Enter fullscreen modeExit fullscreen mode

Nested.then() calls grow quickly and push error handling to the end.

After: async / await

asyncfunctionfetchUser(id){try{constres=awaitfetch(`/api/users/${id}`);constdata=awaitres.json();console.log('User:',data);}catch(err){console.error(err);}}
Enter fullscreen modeExit fullscreen mode

How it works

  • async marks the function, returning a promise automatically.
  • await pauses execution until the promise resolves, yielding the result.
  • try/catch handles both network errors and thrown exceptions.

Sequential vs parallel

// sequentialawaitstepOne();awaitstepTwo();// parallelconst[a,b]=awaitPromise.all([stepOne(),stepTwo()]);
Enter fullscreen modeExit fullscreen mode

UsePromise.all to start tasks together, then await their combined result.

Tips

  • Always wrap awaited code intry/catch for proper error handling.
  • Combine withPromise.allSettled for bulk operations where some may fail.
  • Keepasync functions small; extract chunks to maintain readability.

Async/await became mainstream in 2017, replacing many callback and promise chains with clear top‑to‑bottom control flow.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Web developer, creating stuff on wthe eb and in the real world
  • Location
    Berlin, germany
  • Work
    full stack developer
  • Joined

More fromluiz tanure

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp