Slide 1

Slide 1 text

From Callback Hell To Async Await Tamar Twena-Stern

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Tamar Twena-Stern • Have 3 kids • Community Leader @JavaScript Israel • Loves to play my violin

Slide 4

Slide 4 text

Traditional Approach - Blocking IO Client Server Request Request Request Thread Thread Thread

Slide 5

Slide 5 text

Problems •CPU and memory resources location for every new thread and performs context switching •On stressed – Context switching over actual work

Slide 6

Slide 6 text

Node.js Architecture - Non Blocking IO

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

Asynchronous Code Example

Slide 9

Slide 9 text

So, why callbacks programming is so bad ?

Slide 10

Slide 10 text

The Pyramid Of Doom

Slide 11

Slide 11 text

Easy To Miss Error Handling

Slide 12

Slide 12 text

Correct Error Handling

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Promises

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Promise - Explained

Slide 17

Slide 17 text

Promises Code

Slide 18

Slide 18 text

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 …

Slide 19

Slide 19 text

Lets Measure Some Performance

Slide 20

Slide 20 text

Async Await

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Some Async Await Code

Slide 24

Slide 24 text

How The Situation Is Improved ?

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Clear Error Handling

Slide 27

Slide 27 text

Intermediate Values

Slide 28

Slide 28 text

Parallel Execution Very Easy

Slide 29

Slide 29 text

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!

Slide 30

Slide 30 text

Sequential IO - Example

Slide 31

Slide 31 text

How You Should Write This Code ?

Slide 32

Slide 32 text

Demo : Parallel VS Serial IO Down To Numbers

Slide 33

Slide 33 text

Implement A Promise

Slide 34

Slide 34 text

• Twitter: @SternTwena