JavaScript Promise
Eslam Mahmoud
http://eslam.me
JavaScript evolution
JavaScript evolution
● 90s
○ Netscape, “web needed a way to become more dynamic” Marc Andreessen 1995
○ Mocha > LiveScript > JavaScript (ECMAScript)
○ ECMAScript 2 in 1998 & ECMAScript 3 in 1999
● 2005 Jesse Garrett released a white paper coined the term Ajax (web 2.0)
● 2009 ES5 & Google Chrome and the V8 JavaScript engine
○ JavaScript as a server side language (Node.js)
● 2015 ECMAScript 6 ES6
*https://en.wikipedia.org/wiki/ECMAScript
Synchronous
Synchronous
● Step by step
● You wait for each line to
finish before moving on
● Simple
Synchronous
Asynchronous
Asynchronous
● Parallel execution
● Many requests can
be done
● Not blocked
waiting the other
request to get
response
Asynchronous
Synchronous Asynchronous
JavaScript functions
In JavaScript, functions are first-class objects
functions are of the type Object and they can be used in a first-class manner like
any other object (String, Array, Number, etc.) since they are in fact objects
themselves.
They can be “stored in variables, passed as arguments to functions, created within
functions, and returned from functions
Callback
Is a function that is passed to
another function as a
parameter, and the callback
function is called/executed
inside the otherFunction
Callback
You can get old
before you get
calleback
Handling
Errors
Even
more
error
handling
Make Sure Callback is a Function
Callback hell
Callback hell
I'm not gonna debug this
JavaScript Promises
The Promise object represents the
eventual completion (or failure) of an
asynchronous operation.
I Promise i will return to you when i finish
Promise object
● Status
○ unfulfilled, fulfilled, or failed
● Methods
○ then, catch, reject, resolve, all, race
Creating a Promise
Using
promise
You do not send callback to the Async function to execute it
You call Async function then execute whatever you need
Chaining promises
Chaining
promises
Promises with Synchronous function
Handling
errors
Is it really Asynchronous now !
Promise.all()
My life
before Promises
My life
after Promises
Whats next?
● Async
● Callback
● ES6
● Promises
● Await, yield, generator
● JavaScript As backend (Node.js)
● JavaScript For desktop (electron)
● JavaScript for mobile (phonegap)
Where to go?
Showcase
● JavaScript For desktop (electron)
● JavaScript As backend (Node.js)
ref
● https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_O
bjects/Promise
● https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promis
es
● https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators
/await
● http://es6-features.org
● https://nodejs.org
● https://electron.atom.io
● https://insights.stackoverflow.com/survey/2016
? || //

JavaScript promise