Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Escape Callback Hell and Enter Callback Heaven with Async/Await

Escape Callback Hell and Enter Callback Heaven with Async/Await

JavaScript relies heavily on callbacks for async functions which leads to the infamous 'callback hell'. ES7 is proposing the use of async/await to avoid this. Node.js has already implemented it in version 7.6. I will show you how you can escape from callback hell and reach callback heaven with async await.

Jennifer Bland

September 21, 2017
Tweet

More Decks by Jennifer Bland

Other Decks in Programming

Transcript

  1. Escape Callback Hell and Enter Callback Heaven with Async/Await Jennifer

    Bland
  2. Who Am I? www.jenniferbland.com @ratracegrad www.youtube.com/c/JenniferBland [email protected] github.com/jenniferbland

  3. IN THE BEGINNING or how did we get here

  4. The only thing that maters in software is the experience

    of the user Change How I/O Is Handled
  5. Sample Code

  6. CHANGE THE INFRASTRUCTURE non-blocking event loop

  7. Node.js Design Goals No function should direct perform I/O To

    receive info from disk, network or other process, there must be a callback Stream everything; never force the buffering of data Have built-in support for important protocols
  8. Progression of I/O Handling

  9. Sample Code - no waiting

  10. Sample Code - callback

  11. Sample Code - callback hell

  12. None
  13. Promises

  14. Chaining Promises

  15. z

  16. Async Await Async - declares an asynchronous function Automatically transforms

    a regular function into a Promise When called async functions resolve with whatever is returned in their body Async functions enable the use of await
  17. Async Await Await - pauses the execution of async functions

    When placed in front of a Promise call, await forces the rest of the code to wait until that Promise finishes and returns a result Await works only with Promises, it does not work with callbacks Await can only be used inside async functions
  18. Async/Await Example

  19. Async/Await Detailed Example

  20. Async/Await Handling Errors

  21. Async/Await Handling Errors

  22. Async/Await Handling Errors

  23. Async/Await Examples

  24. Async/Await Examples

  25. Async/Await Examples

  26. Async/Await Examples

  27. Browser Support

  28. Who Am I? www.jenniferbland.com @ratracegrad www.youtube.com/c/JenniferBland [email protected] github.com/jenniferbland