Asynchronous Javascript

Содержание

Слайд 2

Async in JS PLAN Promises Promise magic Async/Await Events & Callbacks

Async in JS

PLAN

Promises

Promise magic

Async/Await

Events & Callbacks

Promise overview

.then(), .catch(), parallel execution

Write async

code as sync!
Слайд 3

ASYNC IN JS

ASYNC IN JS

Слайд 4

EVENTS The most basic form of asynchronous programming in JavaScript.

EVENTS

The most basic form of asynchronous programming in JavaScript.

Слайд 5

CALLBACKS: BASIC Functions that are called after an asynchronous operation Usually are passed as arguments.

CALLBACKS: BASIC

Functions that are called after an asynchronous operation
Usually are passed

as arguments.
Слайд 6

CALLBACKS: HARD

CALLBACKS: HARD

Слайд 7

CALLBACKS: HELL

CALLBACKS: HELL

Слайд 8

PROMISES

PROMISES

Слайд 9

PROMISES Operation that will return its result some time later Multiple

PROMISES

Operation that will return its result some time later
Multiple handlers of

one result
Complex compositions of asynchronous operations
Easier to handle errors
You can "recover" from an error
Слайд 10

.THEN

.THEN

Слайд 11

PROMISES & THENABLES Promises are objects whose behaviour conforms to the

PROMISES & THENABLES

Promises are objects whose behaviour conforms to the Promise

/ A + specification
Thenables - objects that have the .then method.
Слайд 12

CONSTRUCTOR The function passed to new Promise() is called the executor.

CONSTRUCTOR

The function passed to new Promise() is called the executor.
.then ()

and resolve () are called independently.
Слайд 13

PROMISE STATES A Promise is in one of three states: pending:

PROMISE STATES

A Promise is in one of three states:
pending: initial state,

neither fulfilled nor rejected.
fulfilled: meaning that the operation completed successfully.
rejected: meaning that the operation failed.
Слайд 14

WORKING WITH PROMISES

WORKING WITH PROMISES

Слайд 15

.THEN() MAGIC .then() always returns a new promise This new promise

.THEN() MAGIC

.then() always returns a new promise
This new promise is resolved

when the previous one was either completed or rejected.
.then() may not have a handler
The result is transferred to the next promise
If you return a value from the handler in .then(), it will be the value of the next promise
You can transfer data from one promise to the next
Слайд 16

.THEN() & NEW PROMISE If you return a promise with .then

.THEN() & NEW PROMISE

If you return a promise with .then (),

it will be resolved
The result will be wrapped in a new promise and will be available in the next .then()
Слайд 17

ERROR HANDLING Promises can be rejected with reason As a second

ERROR HANDLING

Promises can be rejected with reason
As a second argument, the

executor receives a function that rejects the promise
.then() can receive an error handler as the second argument
Both the completion handler and error handler are optional.
.catch() only accepts an error handler
.catch() is similar to .then() without a completion handler.
Слайд 18

RECOVERY Promises are able to "recover" If you return a value

RECOVERY

Promises are able to "recover"
If you return a value from the

error handler, it will go to the completion handler of the next promise.
Слайд 19

FAIL SILENTLY If an error occurs in a promise that does

FAIL SILENTLY

If an error occurs in a promise that does not

have an error handler, the promise will "keep quiet" about the error.
Слайд 20

CREATING COMPLETED PROMISES Promise.resolve () and Promise.reject () allow you to

CREATING COMPLETED PROMISES

Promise.resolve () and Promise.reject () allow you to create

a completed promise
They can take primitives as an argument and wrap them in a promise.
Слайд 21

BACK TO THENABLES Promise.resolve () and Promise.reject () can take thenable

BACK TO THENABLES

Promise.resolve () and Promise.reject () can take thenable arguments
They

can turn thenables into real promises.
Слайд 22

PARALLEL EXECUTION: ALL Promise.all() is waits for all promises to complete

PARALLEL EXECUTION: ALL

Promise.all() is waits for all promises to complete
Returns a

new promise, which resolves when all promises are completed.
Слайд 23

PARALLEL EXECUTION: RACE Promise.race () creates a race among promises Returns

PARALLEL EXECUTION: RACE

Promise.race () creates a race among promises
Returns a new

promise, which completes when the fastest promise ends.
Слайд 24

ASYNC/AWAIT

ASYNC/AWAIT

Слайд 25

ASYNC/AWAIT ES7 introduced a new way to add async behaviour in

ASYNC/AWAIT

ES7 introduced a new way to add async behaviour in JavaScript
Working

with promises became easier
New async and await keywords
async functions will implicitly return a promise.
Don’t have to create new promises yourself.
Слайд 26

SUSPEND EXECUTION await keyword suspends the asynchronous function and waits for

SUSPEND EXECUTION

await keyword suspends the asynchronous function and waits for the awaited value return

a resolved promise
To get the value of this resolved promise just assign variables to the awaited promise value!
Like we previously did with the then() callback
await doesn’t work in global scope in NodeJS
Слайд 27

ADD SUGAR Async/Await is just syntactic sugar for promises, so you

ADD SUGAR

Async/Await is just syntactic sugar for promises, so you can

treat async function like promises
Слайд 28

ERROR HANDLING Async/await allow us to handle errors the same way

ERROR HANDLING

Async/await allow us to handle errors the same way we

do with synchronous code with try…catch