$30 off During Our Annual Pro Sale. View Details »

From Callback To Async Await

From Callback To Async Await

Tamar Twena-Stern

April 02, 2020

More Decks by Tamar Twena-Stern

Other Decks in Programming


  1. From Callback Hell To Async Await Tamar Twena-Stern

  2. Tamar Twena-Stern • Software Engineer - manager and architect •

    BackEnd Team Leader @XM Cyber • Was a CTO of my own startup • Passionate about Node.js ! • Twitter: @SternTwena
  3. Tamar Twena-Stern • Have 3 kids • Community Leader @JavaScript

    Israel • Loves to play my violin
  4. Traditional Approach - Blocking IO Client Server Request Request Request

    Thread Thread Thread
  5. Problems •CPU and memory resources location for every new thread

    and performs context switching •On stressed – Context switching over actual work
  6. Node.js Architecture - Non Blocking IO

  7. Asynchronous Programming • Synchronous activities will block the event loop.

    • Asynchronous programming has evolved in JavaScript • Functions are first class citizens • No return values, only function calls.
  8. Asynchronous Code Example

  9. So, why callbacks programming is so bad ?

  10. The Pyramid Of Doom

  11. Easy To Miss Error Handling

  12. Correct Error Handling

  13. Different To Implement Control Flows • Very hard to implement

    control flow patterns • You want to be able to execute tasks in : • Series • Parallel • Even implementing array mapping using pure asynchronous functions very hard with plane callbacks
  14. Promises

  15. What Is A Promise • A proxy for a value

    that we don’t know yet • Register an event handler to • Success of an asynchronous operation • Failure of an asynchronous operation • Enable an asynchronous method to return value like a synchronous method
  16. Promise - Explained

  17. Promises Code

  18. Promise Libraries • Native in ES6 • Bluebird – add

    a lot of extra functionality on native ES6 • Promise.mapSeries() • Promise.reduce() • Promise.map() • Promise.Some() • Promise.any() • Promise.each() • Etc …
  19. Lets Measure Some Performance

  20. Async Await

  21. What Is An Async Function • Return an Promise •

    Inside an async function, we can : • Use await before a function that returns a promise • Await waits for the promise to be resolved • Handle errors with easy and popular mechanism – try / catch • Behind the scenes – asynchronous code
  22. Await Keyword • Reserved word from ES7 • Can be

    used only inside an Async function • Await is used to wait for a promise • Await cause the execution of the function to stop • Same concept as generators • Help simplify the code to look synchronous • Behind the scenes – asynchronous code
  23. Some Async Await Code

  24. How The Situation Is Improved ?

  25. Concise And Clean • No need to use .than •

    No need to write anonymous callback function for .than or for .catch • Not creating variables that we will not need • Avoid nesting • Very intuitive
  26. Clear Error Handling

  27. Intermediate Values

  28. Parallel Execution Very Easy

  29. Watch Out • The code is very concise and clean

    • This is miss-leading as the biggest advantages of Node.js are • Non blocking IO • Parallel IO work with the worker thread pool • You have to remember to work as much in parallel as you can!
  30. Sequential IO - Example

  31. How You Should Write This Code ?

  32. Demo : Parallel VS Serial IO Down To Numbers

  33. Implement A Promise

  34. • Twitter: @SternTwena