SlideShare ist ein Scribd-Unternehmen logo
1 von 64
Downloaden Sie, um offline zu lesen
Globalcode – Open4education
JavaScript Promises
Derek Stavis
Engenheiro de Software
Globalcode – Open4education
Who?
Derek Stavis
github.com/derekstavis
Software Engineer
Ruby, JavaScript, Python, C
Node, React & Webpack Advocate
Globalcode – Open4education
How have you been doing
asynchronous JavaScript?
Globalcode – Open4education
How have you been doing
continuation passing?
Globalcode – Open4education
Using callback functions
// In the browser
setTimeout(function () {
// Will be called in the future
}, 2000);
// In the server
fs.readFile('file.txt', function () {
// Will be called when file.txt is read
});
Globalcode – Open4education
Node.js callback standard
fs.readFile('file.txt', function (err, data) {
// If an error occurred, err will have a value
// Always check for errors using if clauses
})
Globalcode – Open4education
Node.js callback scenario
Let’s say we have a fetch function
It does plain HTTP GET
Accepts a URL and a callback
Callback receives error and response
fetch ('url', function (err, res) { })
Globalcode – Open4education
Node.js callback scenario
fetch('/users/session', function (sessionError, user) {
if (sessionError) {
alert('Error fetching session')
return
}
fetch('/users/' + user.id + '/posts', function
(userErr, posts) {
if (userErr) {
alert('Error fetching user posts')
return
}
renderUserPosts(posts)
})
})
Globalcode – Open4education
Node.js callback hell
Globalcode – Open4education
How could we flatten that tree?
Globalcode – Open4education
new Promise()
Globalcode – Open4education
Formal definition
"A promise represents the eventual
result of an asynchronous
operation."
Globalcode – Open4education
Formal definition
"A promise represents the eventual
result of an asynchronous
operation."
Globalcode – Open4education
Enter Promises
An object which represents and
manage the lifecycle of a future
result
Globalcode – Open4education
Promise states
Pending
Fulfilled Rejected
Globalcode – Open4education
Promise states
Promises aren't reusable
Globalcode – Open4education
Promise API
// New Promises start in "Pending" state
new Promise(function (resolve, reject) {
// Transition to "Rejected" state
reject(new Error('A meaningful error'))
// Transition to "Fulfilled" state
resolve({ my: 'data' })
})
Globalcode – Open4education
Promise API
var promise = new Promise(...)
promise.then(function (result) {
console.log(result)
})
=> { my: "data" }
Globalcode – Open4education
Promise API
var promise = new Promise(...)
promise.catch(function (error) {
console.log(error.message)
})
=> A meaningful error
Globalcode – Open4education
Promise API
Node.js callbacks can be easily
wrapped in promises
Globalcode – Open4education
Promise API
function fetch (url) {
return new Promise(function (resolve, reject) {
request(url, function (err, data) {
if (err) {
reject(err)
} else {
resolve(data)
}
})
}
Globalcode – Open4education
Promise API
Every .then and .catch return a
new promise, so promises are
chainable
Globalcode – Open4education
Flattening the tree
function fetchPosts (user) {
return fetch('/users/' + user.id + '/posts')
}
function fetchSession () {
return fetch('/users/session')
}
fetchSession()
.catch(handleSessionError)
.then(fetchPosts)
.catch(handlePostsError)
.then(renderUserPosts)
Globalcode – Open4education
Flattening the tree
Chaining allows flattening the
callback hell and make continuation
passing look sequential
Globalcode – Open4education
Flattening the tree
const fetchTuples = () =>
Promise.resolve([
[1, 1],
[2, 2],
[6, 4]
])
Globalcode – Open4education
Chaining (a.k.a. sequence monad)
const makeObject = e => ({ l: e[0], r: e[1] })
const attachPlus = e => merge(e, { plus: e.l + e.r })
const attachMinus = e => merge(e, { minus: e.l - e.r })
const attachTimes = e => merge(e, { times: e.l * e.r })
const attachDivide = e => merge(e, { divide: e.l / e.r })
fetchTuples()
.then(map(makeObject))
.then(map(attachPlus))
.then(map(attachMinus))
.then(map(attachTimes))
.then(map(attachDivide))
.then(console.log.bind(console))
Globalcode – Open4education
Chaining (a.k.a. sequence monad)
{ l: 1, r: 1, plus: 2, minus: 0, times: 1, divide: 1 }
{ l: 2, r: 2, plus: 4, minus: 0, times: 4, divide: 1 }
{ l: 6, r: 4, plus: 10, minus: 2, times: 24, divide: 1.5 }
Globalcode – Open4education
Promise Implementation
Promise is a specification
Globalcode – Open4education
Promise Implementation
There are a handful of Promise
implementations
Globalcode – Open4education
Promise Implementation
Solving different issues, focusing
on different areas
Globalcode – Open4education
Promise Implementation
So I have to be tied to a single
implementation?
Globalcode – Open4education
NOT HERE
Globalcode – Open4education
Promises/A+ Contract
https://promisesaplus.com
Globalcode – Open4education
Promises/A+ Contract
Promises/A+ provides interface and
behaviour specification for
interoperable promises
Globalcode – Open4education
Promises/A+ Contract
So you are free to use the
implementation which better fit
your needs while keeping your code
compatible
Globalcode – Open4education
Promises/A+ Contract
This contract was created because
there was no native Promise
specification in ECMAScript
Globalcode – Open4education
ECMAScript 2015 Promise
Since ECMAScript 2015 the Promise
object was included in the spec
https://tc39.github.io/ecma262/#sec-promise-
constructor
Globalcode – Open4education
ECMAScript 2015 Promise
It allows more fun stuff do be done
Globalcode – Open4education
ECMAScript 2015 Promise
Waiting for multiple Promises
Globalcode – Open4education
Waiting for multiple Promises
var promises = [
new Promise(function (resolve, reject) {
setTimeout(resolve, 1000);
}),
new Promise(function (resolve, reject) {
setTimeout(resolve, 2000);
})
]
Promise.all(promises).then(function () {
console.log('Ran after 2 seconds')
})
Globalcode – Open4education
ECMAScript 2015 Promise
Racing multiple Promises
Globalcode – Open4education
Racing multiple Promises
var promises = [
new Promise(function (resolve, reject) {
setTimeout(resolve, 1000);
}),
new Promise(function (resolve, reject) {
setTimeout(resolve, 2000);
})
]
Promise.race(promises).then(function () {
console.log('Ran after 1 second')
})
Globalcode – Open4education
You should definitely look into
Promises
Globalcode – Open4education
Because there's even more!
Globalcode – Open4education
Bluebird
A complete Promise library
http://bluebirdjs.com
Globalcode – Open4education
Bluebird Promise
Catch rejections like exceptions
Globalcode – Open4education
Fine-grained exceptions
function SessionError(message) {
this.message = message
this.name = "SessionError"
Error.captureStackTrace(this, SessionError)
}
SessionError.prototype =
Object.create(Error.prototype)
SessionError.prototype.constructor = SessionError
Globalcode – Open4education
Fine-grained exceptions
function fetchPosts (user) {
throw new PostsError('could not fetch posts')
}
function fetchSession () {
return new SessionError('could not fetch session')
}
fetchSession()
.then(fetchPosts)
.then(renderPosts)
.catch(SessionError, handleSessionError)
.catch(PostsError, handlePostsError)
Globalcode – Open4education
Bluebird Promise
Spread Promise.all result as
parameters
Globalcode – Open4education
Parameter spread
Promise.all([
download('http://foo.bar/file.tar.gz'),
download('http://foo.bar/file.tar.gz.sig')
]).spread((file, signature) =>
sign(file) == signature
? Promise.resolve(file)
: Promise.reject('invalid signature')
)
Globalcode – Open4education
Bluebird Promise
Use .all & .spread for dynamic
amount of promises
When doing fixed number of
promises use .join
Globalcode – Open4education
Join promises results
Promise.join(
download('http://foo.bar/file.tar.gz'),
download('http://foo.bar/file.tar.gz.sig'),
(file, signature) =>
sign(file) == signature
? Promise.resolve(file)
: Promise.reject('invalid signature')
)
Globalcode – Open4education
Bluebird Promise
Resolve objects with promises
Globalcode – Open4education
Join promises results
Promise.props({
file: download('http://foo.bar/file.tar.gz'),
sig: download('http://foo.bar/file.tar.gz.sig')
}).then(data =>
sign(data.file) == data.sig
? Promise.resolve(file)
: Promise.reject('invalid signature')
)
Globalcode – Open4education
Bluebird Promise
Do some .reduce with promises
Globalcode – Open4education
Reduce promises results
const urls = fetchProjects()
Promise.reduce(urls, (total, url) =>
fetch(url).then(data =>
total + data.stars), 0)
Globalcode – Open4education
HTML Fetch
A Promise approach to HTTP requests
https://fetch.spec.whatwg.org
Globalcode – Open4education
#DeprecateXHR
fetch('/users.json')
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json)
}).catch(function(ex) {
console.log('parsing failed', ex)
})
Globalcode – Open4education
#DeprecateXHR
fetch('/users.json')
.then(function(response) {
return response.json()
fetch('/users.json')
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json)
}).catch(function(ex) {
console.log('parsing failed', ex)
})
Globalcode – Open4education
#DeprecateXHR
fetch is growing so powerful
Globalcode – Open4education
#DeprecateXHR
$ telnet mockbin.org 80
GET /bin/2294df68-ae10-4336-a732-3170597543a9 HTTP/1.1
Accept: */*
Host: mockbin.org
HTTP/1.1 200 OK
Content-Type: text/html
Custom-Header: CustomValue
{"fetch": "is so cool"}
Globalcode – Open4education
#DeprecateXHR
fetch promise resolve as soon as
headers are ready
Globalcode – Open4education
Demo
Fetching stuff from Github
https://github.com/derekstavis/
promises-on-the-browser
Globalcode – Open4education
Thanks for watching
Questions?
github.com/derekstavis
twitter.com/derekstavis
facebook.com/derekstavis

Weitere Àhnliche Inhalte

Was ist angesagt?

JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
Jussi Pohjolainen
 

Was ist angesagt? (20)

NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
The New JavaScript: ES6
The New JavaScript: ES6The New JavaScript: ES6
The New JavaScript: ES6
 
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modules
 
Rxjs ppt
Rxjs pptRxjs ppt
Rxjs ppt
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Closures in Javascript
Closures in JavascriptClosures in Javascript
Closures in Javascript
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
 
Meetup angular http client
Meetup angular http clientMeetup angular http client
Meetup angular http client
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
 
Why TypeScript?
Why TypeScript?Why TypeScript?
Why TypeScript?
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java Developers
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
React JS
React JSReact JS
React JS
 
Node js overview
Node js overviewNode js overview
Node js overview
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Angular tutorial
Angular tutorialAngular tutorial
Angular tutorial
 

Andere mochten auch

Andere mochten auch (8)

Awesomely descriptive JavaScript with monads
Awesomely descriptive JavaScript with monadsAwesomely descriptive JavaScript with monads
Awesomely descriptive JavaScript with monads
 
Practical JavaScript Promises
Practical JavaScript PromisesPractical JavaScript Promises
Practical JavaScript Promises
 
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
 
Introdução ao MongoDB em 30 slides
Introdução ao MongoDB em 30 slidesIntrodução ao MongoDB em 30 slides
Introdução ao MongoDB em 30 slides
 
ECMAScript 6 and the Node Driver
ECMAScript 6 and the Node DriverECMAScript 6 and the Node Driver
ECMAScript 6 and the Node Driver
 
Utilizing Bluebird Promises
Utilizing Bluebird PromisesUtilizing Bluebird Promises
Utilizing Bluebird Promises
 
Promise and Bluebird
Promise and BluebirdPromise and Bluebird
Promise and Bluebird
 
Packing it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to nowPacking it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to now
 

Ähnlich wie JavaScript Promises

Promises look into the async future
Promises look into the async futurePromises look into the async future
Promises look into the async future
slicejs
 
Java EE 7: Boosting Productivity and Embracing HTML5
Java EE 7: Boosting Productivity and Embracing HTML5Java EE 7: Boosting Productivity and Embracing HTML5
Java EE 7: Boosting Productivity and Embracing HTML5
Arun Gupta
 

Ähnlich wie JavaScript Promises (20)

The Promised Land (in Angular)
The Promised Land (in Angular)The Promised Land (in Angular)
The Promised Land (in Angular)
 
Unit Testing Express Middleware
Unit Testing Express MiddlewareUnit Testing Express Middleware
Unit Testing Express Middleware
 
Leveraging JavaScript Promises and the Bulk API
Leveraging JavaScript Promises and the Bulk APILeveraging JavaScript Promises and the Bulk API
Leveraging JavaScript Promises and the Bulk API
 
Timings API: Performance Assertion during the functional testing
 Timings API: Performance Assertion during the functional testing Timings API: Performance Assertion during the functional testing
Timings API: Performance Assertion during the functional testing
 
Promises look into the async future
Promises look into the async futurePromises look into the async future
Promises look into the async future
 
Release with confidence
Release with confidenceRelease with confidence
Release with confidence
 
Solving anything in VCL
Solving anything in VCLSolving anything in VCL
Solving anything in VCL
 
Altitude San Francisco 2018: Testing with Fastly Workshop
Altitude San Francisco 2018: Testing with Fastly WorkshopAltitude San Francisco 2018: Testing with Fastly Workshop
Altitude San Francisco 2018: Testing with Fastly Workshop
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Testing your application on Google App Engine
Testing your application on Google App EngineTesting your application on Google App Engine
Testing your application on Google App Engine
 
Testing Your Application On Google App Engine
Testing Your Application On Google App EngineTesting Your Application On Google App Engine
Testing Your Application On Google App Engine
 
Node.js cluster
Node.js clusterNode.js cluster
Node.js cluster
 
C#on linux
C#on linuxC#on linux
C#on linux
 
Javascript Promises/Q Library
Javascript Promises/Q LibraryJavascript Promises/Q Library
Javascript Promises/Q Library
 
Real World Lessons on the Pain Points of Node.js Applications
Real World Lessons on the Pain Points of Node.js ApplicationsReal World Lessons on the Pain Points of Node.js Applications
Real World Lessons on the Pain Points of Node.js Applications
 
Java EE 7 Platform: Boosting Productivity and Embracing HTML5 - Arun Gupta (R...
Java EE 7 Platform: Boosting Productivity and Embracing HTML5 - Arun Gupta (R...Java EE 7 Platform: Boosting Productivity and Embracing HTML5 - Arun Gupta (R...
Java EE 7 Platform: Boosting Productivity and Embracing HTML5 - Arun Gupta (R...
 
Java EE 7: Boosting Productivity and Embracing HTML5
Java EE 7: Boosting Productivity and Embracing HTML5Java EE 7: Boosting Productivity and Embracing HTML5
Java EE 7: Boosting Productivity and Embracing HTML5
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5
 
JDD2015: Kubernetes - Beyond the basics - Paul Bakker
JDD2015: Kubernetes - Beyond the basics - Paul BakkerJDD2015: Kubernetes - Beyond the basics - Paul Bakker
JDD2015: Kubernetes - Beyond the basics - Paul Bakker
 
PHP as a Service TDC2019
PHP as a Service TDC2019PHP as a Service TDC2019
PHP as a Service TDC2019
 

KĂŒrzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Enterprise Knowledge
 

KĂŒrzlich hochgeladen (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

JavaScript Promises