SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
Async JavaScript in ES7
Modern Web UI Meetup
http://www.modernwebui.org
5/13/2015 @ Mozilla HQ
Who is Mike North?
2 @MichaelLNorth
UI Engineering Lead
@MichaelLNorth
github.com/truenorth
Blocking vs. Waiting
3
▪ Some people think of this as “push” vs “pull”
@MichaelLNorth
var weather = getForecast();
getForecast(function (weather) { ... });
▪ Blocking
▪ Waiting
Sync - Blocking is easy
4
▪ Code is simple

▪ Error handling with try/catch

▪ We only have one thread
@MichaelLNorth
function getForecast() {
let temperature = getTemperatureData();
let humidity = getHumidityData();
return {temperature, humidity};
}
Async - Callbacks
5
▪ See: Node.js

▪ Increased
complexity

▪ Explicit error
handling
@MichaelLNorth
function getForecast(cb) {
getTemperatureData( temperature => {
getHumidityData( humidity => {
cb({
temperature,
humidity
});
}, error => cb(error));
}, error => cb(error));
}
Async - Callbacks
6 @MichaelLNorth
Async - Promises
7
▪ Better error
handling model

▪ Still doesn’t look
like our “blocking”
code

▪ Promise-ness leaks
out everywhere
@MichaelLNorth
function getForecast() {
return
getTemperatureData(temperature => {
getHumidityData(humidity => {
return {
temperature,
humidity
};
})
})
}
Some cool stuff coming in 2016
that you can use right now
Iterators - Data Producers
9
▪ A convention that all ES6 Collections will implement
@MichaelLNorth
let it = getCollection()[Symbol.iterator]();
it.next(); // {value: 64, done: false}
it.next(); // {value: 68, done: false}
it.next(); // {value: undefined, done: true}
For-Of Loop
10 @MichaelLNorth
let a = [1,2,3,4,5];
for (let y of a) {
console.log(y);
}
let a = [1,2,3,4,5];
let it = a[Symbol.iterator]();
for(var i = it.next();
!i.done;
i = it.next()) {
console.log(i.value);
}
Generators - fns that return many values
11 @MichaelLNorth
function* getValues() {
yield 64;
yield 68;
return 71;
}
var values = getValues();
values.next(); // {value: 64, done: false}
values.next(); // {value: 68, done: false}
values.next(); // {value: 71, done: true}
▪ Generators return an iterator
Generators - fns that return many values
12 @MichaelLNorth
function* getValues() {
yield 64;
yield 68;
return 71;
}
▪ execution suspends @ yield

▪ values can be passed into a
generator
function* getValues() {
var x = yield 64;
var y = yield 2*x;
return 3*y;
}
it.next(31);
Generators - fns that return many values
13 @MichaelLNorth
function* getValues() {
yield 64;
yield 68;
return 71;
}
▪ execution suspends @ yield

▪ values can be passed into a
generator
function* getValues() {
var x = yield 64;
var y = yield 2*x;
return 3*y;
}
it.next(31);
▪ can iterate infinitely
Generators - fns that return many values
14 @MichaelLNorth
function* getValues() {
var x = yield 64;
var y = yield 2*x;
return 3*y;
}
var it = getValues();
Generators - fns that return many values
15 @MichaelLNorth
function* getValues() {
var x = yield 64;
var y = yield 2*x;
return 3*y;
}
var it = getValues();
it.next();
> {value: 64, done: false}
Generators - fns that return many values
16 @MichaelLNorth
function* getValues() {
var x = yield 64 6;
var y = yield 2*x;
return 3*y;
}
var it = getValues();
it.next();
> {value: 64, done: false}
it.next(6);
> {value: 12, done: false}
Generators - fns that return many values
17 @MichaelLNorth
function* getValues() {
var x = yield 64 6;
var y = yield 2*x 3;
return 3*y;
}
var it = getValues();
it.next();
> {value: 64, done: false}
it.next(6);
> {value: 12, done: false}
it.next(3);
> {value: 9, done: true}
@MichaelLNorth18
Async Iteration
19 @MichaelLNorth
function spawn(generator) {
return new Promise((resolve, reject) => {
var resultHandler = lastResult => {
var {value, done} = generator.next(lastResult);
if (!done) {
value.then(resultHandler, reject);
}
else {
accept(value);
}
};
});
}
@MichaelLNorth20
function spawn(generator) {
return new Promise((resolve, reject) => {
var resultHandler = lastResult => {
var {value, done} = generator.next(lastResult);
if (!done) {
value.then(resultHandler, reject);
}
else {
accept(value);
}
};
});
}
function* getWeather(zip) {
var temperature = yield getTemperature(zip);
var humidity = yield getHumidity(zip);
return {temperature, humidity};
}
spawn(getWeather(zip)).then(weather => console.log(weather));
@MichaelLNorth21
function spawn(generator) {
return new Promise((resolve, reject) => {
var resultHandler = lastResult => {
var {value, done} = generator.next(lastResult);
if (!done) {
value.then(resultHandler, reject);
}
else {
accept(value);
}
};
});
}
function* getWeather(zip) {
var temperature = yield getTemperature(zip);
var humidity = yield getHumidity(zip);
return {temperature, humidity};
}
spawn(getWeather(zip)).then(weather => console.log(weather));
Promise
@MichaelLNorth22
function spawn(generator) {
return new Promise((resolve, reject) => {
var resultHandler = lastResult => {
var {value, done} = generator.next(lastResult);
if (!done) {
value.then(resultHandler, reject);
}
else {
accept(value);
}
};
});
}
function* getWeather(zip) {
var temperature = yield getTemperature(zip) 72;
var humidity = yield getHumidity(zip);
return {temperature, humidity};
}
spawn(getWeather(zip)).then(weather => console.log(weather));
Promise
Value
@MichaelLNorth23
function spawn(generator) {
return new Promise((resolve, reject) => {
var resultHandler = lastResult => {
var {value, done} = generator.next(lastResult);
if (!done) {
value.then(resultHandler, reject);
}
else {
accept(value);
}
};
});
}
function* getWeather(zip) {
var temperature = yield getTemperature(zip) 72;
var humidity = yield getHumidity(zip) 0.32;
return {temperature, humidity};
}
spawn(getWeather(zip)).then(weather => console.log(weather));
Value
@MichaelLNorth
But wait…
24
function* getWeather(zip) {
var temperature = yield getTemperature(zip);
var humidity = yield getHumidity(zip);
return {temperature, humidity};
}
async function getWeather(zip) {
var temperature = await getTemperature(zip);
var humidity = await getHumidity(zip);
return {temperature, humidity};
}
function getWeather(zip) {
var temperature = getTemperature(zip);
var humidity = getHumidity(zip);
return {temperature, humidity};
}
Use it today
25 @MichaelLNorth
babel: {
nonStandard: false,
optional: [
'es7.asyncFunctions'
]
}
regeneratorRuntime.wrap(function getValues$
(context) {
while (1) switch (context.prev = context.next) {
case 0:
context.next = 2;
return 1;
case 2:
context.next = 4;
return 2;
case 4:
return context.abrupt("return", 31);
case 5:
case "end":
return context.stop();
}
}, ...);
function *getValues() {
yield 1;
yield 2;
return 31;
}
@MichaelLNorth
Learn More
26
Netflix JavaScript Talks - Version 7: The Evolution of JavaScript

Contenu connexe

Tendances

Behind modern concurrency primitives
Behind modern concurrency primitivesBehind modern concurrency primitives
Behind modern concurrency primitivesBartosz Sypytkowski
 
The Ring programming language version 1.9 book - Part 92 of 210
The Ring programming language version 1.9 book - Part 92 of 210The Ring programming language version 1.9 book - Part 92 of 210
The Ring programming language version 1.9 book - Part 92 of 210Mahmoud Samir Fayed
 
Why Redux-Observable?
Why Redux-Observable?Why Redux-Observable?
Why Redux-Observable?Anna Su
 
Understanding reactive programming with microsoft reactive extensions
Understanding reactive programming  with microsoft reactive extensionsUnderstanding reactive programming  with microsoft reactive extensions
Understanding reactive programming with microsoft reactive extensionsOleksandr Zhevzhyk
 
Avoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promisesAvoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promisesAnkit Agarwal
 
Android getting started
Android getting startedAndroid getting started
Android getting startedUptech
 
Correcting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NETCorrecting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NETBrandon Minnick, MBA
 
Building serverless application on the Apache Openwhisk platform
Building serverless application on the Apache Openwhisk platformBuilding serverless application on the Apache Openwhisk platform
Building serverless application on the Apache Openwhisk platformLucio Grenzi
 
MongoDB World 2019: Life In Stitch-es
MongoDB World 2019: Life In Stitch-esMongoDB World 2019: Life In Stitch-es
MongoDB World 2019: Life In Stitch-esMongoDB
 
Driving User Engagement with watchOS 3
Driving User Engagement with watchOS 3Driving User Engagement with watchOS 3
Driving User Engagement with watchOS 3Kristina Fox
 
Asynchronní programování
Asynchronní programováníAsynchronní programování
Asynchronní programováníPeckaDesign.cz
 
Orchestrate Event-Driven Infrastructure with SaltStack
Orchestrate Event-Driven Infrastructure with SaltStackOrchestrate Event-Driven Infrastructure with SaltStack
Orchestrate Event-Driven Infrastructure with SaltStackLove Nyberg
 
Intelligent infrastructure with SaltStack
Intelligent infrastructure with SaltStackIntelligent infrastructure with SaltStack
Intelligent infrastructure with SaltStackLove Nyberg
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsPiotr Pelczar
 
RxJS In-Depth - AngularConnect 2015
RxJS In-Depth - AngularConnect 2015RxJS In-Depth - AngularConnect 2015
RxJS In-Depth - AngularConnect 2015Ben Lesh
 
Correcting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await MistakesCorrecting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await MistakesBrandon Minnick, MBA
 
FullStack Reativo com Spring WebFlux + Angular
FullStack Reativo com Spring WebFlux + AngularFullStack Reativo com Spring WebFlux + Angular
FullStack Reativo com Spring WebFlux + AngularLoiane Groner
 
Pipeline interface
Pipeline interfacePipeline interface
Pipeline interfaceallanh0526
 

Tendances (20)

Behind modern concurrency primitives
Behind modern concurrency primitivesBehind modern concurrency primitives
Behind modern concurrency primitives
 
The Ring programming language version 1.9 book - Part 92 of 210
The Ring programming language version 1.9 book - Part 92 of 210The Ring programming language version 1.9 book - Part 92 of 210
The Ring programming language version 1.9 book - Part 92 of 210
 
Why Redux-Observable?
Why Redux-Observable?Why Redux-Observable?
Why Redux-Observable?
 
Understanding reactive programming with microsoft reactive extensions
Understanding reactive programming  with microsoft reactive extensionsUnderstanding reactive programming  with microsoft reactive extensions
Understanding reactive programming with microsoft reactive extensions
 
Gwt RPC
Gwt RPCGwt RPC
Gwt RPC
 
Avoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promisesAvoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promises
 
Android getting started
Android getting startedAndroid getting started
Android getting started
 
Correcting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NETCorrecting Common Async/Await Mistakes in .NET
Correcting Common Async/Await Mistakes in .NET
 
Building serverless application on the Apache Openwhisk platform
Building serverless application on the Apache Openwhisk platformBuilding serverless application on the Apache Openwhisk platform
Building serverless application on the Apache Openwhisk platform
 
MongoDB World 2019: Life In Stitch-es
MongoDB World 2019: Life In Stitch-esMongoDB World 2019: Life In Stitch-es
MongoDB World 2019: Life In Stitch-es
 
Driving User Engagement with watchOS 3
Driving User Engagement with watchOS 3Driving User Engagement with watchOS 3
Driving User Engagement with watchOS 3
 
Asynchronní programování
Asynchronní programováníAsynchronní programování
Asynchronní programování
 
Orchestrate Event-Driven Infrastructure with SaltStack
Orchestrate Event-Driven Infrastructure with SaltStackOrchestrate Event-Driven Infrastructure with SaltStack
Orchestrate Event-Driven Infrastructure with SaltStack
 
Intelligent infrastructure with SaltStack
Intelligent infrastructure with SaltStackIntelligent infrastructure with SaltStack
Intelligent infrastructure with SaltStack
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.js
 
RxJS In-Depth - AngularConnect 2015
RxJS In-Depth - AngularConnect 2015RxJS In-Depth - AngularConnect 2015
RxJS In-Depth - AngularConnect 2015
 
Correcting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await MistakesCorrecting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await Mistakes
 
FullStack Reativo com Spring WebFlux + Angular
FullStack Reativo com Spring WebFlux + AngularFullStack Reativo com Spring WebFlux + Angular
FullStack Reativo com Spring WebFlux + Angular
 
Reduxing like a pro
Reduxing like a proReduxing like a pro
Reduxing like a pro
 
Pipeline interface
Pipeline interfacePipeline interface
Pipeline interface
 

Similaire à Async JavaScript in ES7

Code level change propagation in virtual platform
Code level change propagation in virtual platformCode level change propagation in virtual platform
Code level change propagation in virtual platformHaiderAli650468
 
The Promised Land (in Angular)
The Promised Land (in Angular)The Promised Land (in Angular)
The Promised Land (in Angular)Domenic Denicola
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesAnkit Rastogi
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo Ali Parmaksiz
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_functiontimotheeg
 
Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS ProgrammersDavid Rodenas
 
Migrating from Flux to Redux. Why and how.
Migrating from Flux to Redux. Why and how.Migrating from Flux to Redux. Why and how.
Migrating from Flux to Redux. Why and how.Astrails
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on AndroidTomáš Kypta
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Lin Yo-An
 
Processing large-scale graphs with Google(TM) Pregel by MICHAEL HACKSTEIN at...
 Processing large-scale graphs with Google(TM) Pregel by MICHAEL HACKSTEIN at... Processing large-scale graphs with Google(TM) Pregel by MICHAEL HACKSTEIN at...
Processing large-scale graphs with Google(TM) Pregel by MICHAEL HACKSTEIN at...Big Data Spain
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...DroidConTLV
 
Construire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleConstruire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleThierry Wasylczenko
 
The redux saga begins
The redux saga beginsThe redux saga begins
The redux saga beginsDaniel Franz
 
ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation JavascriptRamesh Nair
 
Repetition is bad, repetition is bad.
Repetition is bad, repetition is bad.Repetition is bad, repetition is bad.
Repetition is bad, repetition is bad.wellD
 
Repetition is bad, repetition is bad.
Repetition is bad, repetition is bad.Repetition is bad, repetition is bad.
Repetition is bad, repetition is bad.Michele Giacobazzi
 
Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)RichardWarburton
 
Promise of an API
Promise of an APIPromise of an API
Promise of an APIMaxim Zaks
 

Similaire à Async JavaScript in ES7 (20)

Code level change propagation in virtual platform
Code level change propagation in virtual platformCode level change propagation in virtual platform
Code level change propagation in virtual platform
 
The Promised Land (in Angular)
The Promised Land (in Angular)The Promised Land (in Angular)
The Promised Land (in Angular)
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
 
Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS Programmers
 
Migrating from Flux to Redux. Why and how.
Migrating from Flux to Redux. Why and how.Migrating from Flux to Redux. Why and how.
Migrating from Flux to Redux. Why and how.
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
 
Processing large-scale graphs with Google(TM) Pregel by MICHAEL HACKSTEIN at...
 Processing large-scale graphs with Google(TM) Pregel by MICHAEL HACKSTEIN at... Processing large-scale graphs with Google(TM) Pregel by MICHAEL HACKSTEIN at...
Processing large-scale graphs with Google(TM) Pregel by MICHAEL HACKSTEIN at...
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
 
Construire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleConstruire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradle
 
The redux saga begins
The redux saga beginsThe redux saga begins
The redux saga begins
 
ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation Javascript
 
Repetition is bad, repetition is bad.
Repetition is bad, repetition is bad.Repetition is bad, repetition is bad.
Repetition is bad, repetition is bad.
 
Repetition is bad, repetition is bad.
Repetition is bad, repetition is bad.Repetition is bad, repetition is bad.
Repetition is bad, repetition is bad.
 
Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)
 
Promise of an API
Promise of an APIPromise of an API
Promise of an API
 

Plus de Mike North

Web Security: A Primer for Developers
Web Security: A Primer for DevelopersWeb Security: A Primer for Developers
Web Security: A Primer for DevelopersMike North
 
A Debugging Adventure: Journey through Ember.js Glue
A Debugging Adventure: Journey through Ember.js GlueA Debugging Adventure: Journey through Ember.js Glue
A Debugging Adventure: Journey through Ember.js GlueMike North
 
Anatomy of a Progressive Web App
Anatomy of a Progressive Web AppAnatomy of a Progressive Web App
Anatomy of a Progressive Web AppMike North
 
Web and Native: Bridging the Gap
Web and Native: Bridging the GapWeb and Native: Bridging the Gap
Web and Native: Bridging the GapMike North
 
The Road to Native Web Components
The Road to Native Web ComponentsThe Road to Native Web Components
The Road to Native Web ComponentsMike North
 
Enemy of the state
Enemy of the stateEnemy of the state
Enemy of the stateMike North
 
Phoenix for Rubyists - Rubyconf Brazil 2016
Phoenix for Rubyists - Rubyconf Brazil 2016Phoenix for Rubyists - Rubyconf Brazil 2016
Phoenix for Rubyists - Rubyconf Brazil 2016Mike North
 
Phoenix for Rubyists
Phoenix for RubyistsPhoenix for Rubyists
Phoenix for RubyistsMike North
 
Write Once, Run Everywhere - Ember.js Munich
Write Once, Run Everywhere - Ember.js MunichWrite Once, Run Everywhere - Ember.js Munich
Write Once, Run Everywhere - Ember.js MunichMike North
 
Delightful UX for Distributed Systems
Delightful UX for Distributed SystemsDelightful UX for Distributed Systems
Delightful UX for Distributed SystemsMike North
 
Modern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsModern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsMike North
 
Ember addons, served three ways
Ember addons, served three waysEmber addons, served three ways
Ember addons, served three waysMike North
 
CI/CD and Asset Serving for Single Page Apps
CI/CD and Asset Serving for Single Page AppsCI/CD and Asset Serving for Single Page Apps
CI/CD and Asset Serving for Single Page AppsMike North
 
User percieved performance
User percieved performanceUser percieved performance
User percieved performanceMike North
 
User-percieved performance
User-percieved performanceUser-percieved performance
User-percieved performanceMike North
 
Write Once, Run Everywhere
Write Once, Run EverywhereWrite Once, Run Everywhere
Write Once, Run EverywhereMike North
 
Compose all the things (Wicked Good Ember 2015)
Compose all the things (Wicked Good Ember 2015)Compose all the things (Wicked Good Ember 2015)
Compose all the things (Wicked Good Ember 2015)Mike North
 
Test like a pro with Ember.js
Test like a pro with Ember.jsTest like a pro with Ember.js
Test like a pro with Ember.jsMike North
 
Modern Web UI - Web components
Modern Web UI - Web componentsModern Web UI - Web components
Modern Web UI - Web componentsMike North
 

Plus de Mike North (19)

Web Security: A Primer for Developers
Web Security: A Primer for DevelopersWeb Security: A Primer for Developers
Web Security: A Primer for Developers
 
A Debugging Adventure: Journey through Ember.js Glue
A Debugging Adventure: Journey through Ember.js GlueA Debugging Adventure: Journey through Ember.js Glue
A Debugging Adventure: Journey through Ember.js Glue
 
Anatomy of a Progressive Web App
Anatomy of a Progressive Web AppAnatomy of a Progressive Web App
Anatomy of a Progressive Web App
 
Web and Native: Bridging the Gap
Web and Native: Bridging the GapWeb and Native: Bridging the Gap
Web and Native: Bridging the Gap
 
The Road to Native Web Components
The Road to Native Web ComponentsThe Road to Native Web Components
The Road to Native Web Components
 
Enemy of the state
Enemy of the stateEnemy of the state
Enemy of the state
 
Phoenix for Rubyists - Rubyconf Brazil 2016
Phoenix for Rubyists - Rubyconf Brazil 2016Phoenix for Rubyists - Rubyconf Brazil 2016
Phoenix for Rubyists - Rubyconf Brazil 2016
 
Phoenix for Rubyists
Phoenix for RubyistsPhoenix for Rubyists
Phoenix for Rubyists
 
Write Once, Run Everywhere - Ember.js Munich
Write Once, Run Everywhere - Ember.js MunichWrite Once, Run Everywhere - Ember.js Munich
Write Once, Run Everywhere - Ember.js Munich
 
Delightful UX for Distributed Systems
Delightful UX for Distributed SystemsDelightful UX for Distributed Systems
Delightful UX for Distributed Systems
 
Modern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsModern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.js
 
Ember addons, served three ways
Ember addons, served three waysEmber addons, served three ways
Ember addons, served three ways
 
CI/CD and Asset Serving for Single Page Apps
CI/CD and Asset Serving for Single Page AppsCI/CD and Asset Serving for Single Page Apps
CI/CD and Asset Serving for Single Page Apps
 
User percieved performance
User percieved performanceUser percieved performance
User percieved performance
 
User-percieved performance
User-percieved performanceUser-percieved performance
User-percieved performance
 
Write Once, Run Everywhere
Write Once, Run EverywhereWrite Once, Run Everywhere
Write Once, Run Everywhere
 
Compose all the things (Wicked Good Ember 2015)
Compose all the things (Wicked Good Ember 2015)Compose all the things (Wicked Good Ember 2015)
Compose all the things (Wicked Good Ember 2015)
 
Test like a pro with Ember.js
Test like a pro with Ember.jsTest like a pro with Ember.js
Test like a pro with Ember.js
 
Modern Web UI - Web components
Modern Web UI - Web componentsModern Web UI - Web components
Modern Web UI - Web components
 

Dernier

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
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 WorkerThousandEyes
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
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 FresherRemote DBA Services
 
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 2024The Digital Insurer
 
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 textsMaria Levchenko
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
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 slidevu2urc
 
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 WorkerThousandEyes
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
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...Miguel Araújo
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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?Antenna Manufacturer Coco
 

Dernier (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
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
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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...
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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?
 

Async JavaScript in ES7

  • 1. Async JavaScript in ES7 Modern Web UI Meetup http://www.modernwebui.org 5/13/2015 @ Mozilla HQ
  • 2. Who is Mike North? 2 @MichaelLNorth UI Engineering Lead @MichaelLNorth github.com/truenorth
  • 3. Blocking vs. Waiting 3 ▪ Some people think of this as “push” vs “pull” @MichaelLNorth var weather = getForecast(); getForecast(function (weather) { ... }); ▪ Blocking ▪ Waiting
  • 4. Sync - Blocking is easy 4 ▪ Code is simple ▪ Error handling with try/catch ▪ We only have one thread @MichaelLNorth function getForecast() { let temperature = getTemperatureData(); let humidity = getHumidityData(); return {temperature, humidity}; }
  • 5. Async - Callbacks 5 ▪ See: Node.js ▪ Increased complexity ▪ Explicit error handling @MichaelLNorth function getForecast(cb) { getTemperatureData( temperature => { getHumidityData( humidity => { cb({ temperature, humidity }); }, error => cb(error)); }, error => cb(error)); }
  • 6. Async - Callbacks 6 @MichaelLNorth
  • 7. Async - Promises 7 ▪ Better error handling model ▪ Still doesn’t look like our “blocking” code ▪ Promise-ness leaks out everywhere @MichaelLNorth function getForecast() { return getTemperatureData(temperature => { getHumidityData(humidity => { return { temperature, humidity }; }) }) }
  • 8. Some cool stuff coming in 2016 that you can use right now
  • 9. Iterators - Data Producers 9 ▪ A convention that all ES6 Collections will implement @MichaelLNorth let it = getCollection()[Symbol.iterator](); it.next(); // {value: 64, done: false} it.next(); // {value: 68, done: false} it.next(); // {value: undefined, done: true}
  • 10. For-Of Loop 10 @MichaelLNorth let a = [1,2,3,4,5]; for (let y of a) { console.log(y); } let a = [1,2,3,4,5]; let it = a[Symbol.iterator](); for(var i = it.next(); !i.done; i = it.next()) { console.log(i.value); }
  • 11. Generators - fns that return many values 11 @MichaelLNorth function* getValues() { yield 64; yield 68; return 71; } var values = getValues(); values.next(); // {value: 64, done: false} values.next(); // {value: 68, done: false} values.next(); // {value: 71, done: true} ▪ Generators return an iterator
  • 12. Generators - fns that return many values 12 @MichaelLNorth function* getValues() { yield 64; yield 68; return 71; } ▪ execution suspends @ yield ▪ values can be passed into a generator function* getValues() { var x = yield 64; var y = yield 2*x; return 3*y; } it.next(31);
  • 13. Generators - fns that return many values 13 @MichaelLNorth function* getValues() { yield 64; yield 68; return 71; } ▪ execution suspends @ yield ▪ values can be passed into a generator function* getValues() { var x = yield 64; var y = yield 2*x; return 3*y; } it.next(31); ▪ can iterate infinitely
  • 14. Generators - fns that return many values 14 @MichaelLNorth function* getValues() { var x = yield 64; var y = yield 2*x; return 3*y; } var it = getValues();
  • 15. Generators - fns that return many values 15 @MichaelLNorth function* getValues() { var x = yield 64; var y = yield 2*x; return 3*y; } var it = getValues(); it.next(); > {value: 64, done: false}
  • 16. Generators - fns that return many values 16 @MichaelLNorth function* getValues() { var x = yield 64 6; var y = yield 2*x; return 3*y; } var it = getValues(); it.next(); > {value: 64, done: false} it.next(6); > {value: 12, done: false}
  • 17. Generators - fns that return many values 17 @MichaelLNorth function* getValues() { var x = yield 64 6; var y = yield 2*x 3; return 3*y; } var it = getValues(); it.next(); > {value: 64, done: false} it.next(6); > {value: 12, done: false} it.next(3); > {value: 9, done: true}
  • 19. Async Iteration 19 @MichaelLNorth function spawn(generator) { return new Promise((resolve, reject) => { var resultHandler = lastResult => { var {value, done} = generator.next(lastResult); if (!done) { value.then(resultHandler, reject); } else { accept(value); } }; }); }
  • 20. @MichaelLNorth20 function spawn(generator) { return new Promise((resolve, reject) => { var resultHandler = lastResult => { var {value, done} = generator.next(lastResult); if (!done) { value.then(resultHandler, reject); } else { accept(value); } }; }); } function* getWeather(zip) { var temperature = yield getTemperature(zip); var humidity = yield getHumidity(zip); return {temperature, humidity}; } spawn(getWeather(zip)).then(weather => console.log(weather));
  • 21. @MichaelLNorth21 function spawn(generator) { return new Promise((resolve, reject) => { var resultHandler = lastResult => { var {value, done} = generator.next(lastResult); if (!done) { value.then(resultHandler, reject); } else { accept(value); } }; }); } function* getWeather(zip) { var temperature = yield getTemperature(zip); var humidity = yield getHumidity(zip); return {temperature, humidity}; } spawn(getWeather(zip)).then(weather => console.log(weather)); Promise
  • 22. @MichaelLNorth22 function spawn(generator) { return new Promise((resolve, reject) => { var resultHandler = lastResult => { var {value, done} = generator.next(lastResult); if (!done) { value.then(resultHandler, reject); } else { accept(value); } }; }); } function* getWeather(zip) { var temperature = yield getTemperature(zip) 72; var humidity = yield getHumidity(zip); return {temperature, humidity}; } spawn(getWeather(zip)).then(weather => console.log(weather)); Promise Value
  • 23. @MichaelLNorth23 function spawn(generator) { return new Promise((resolve, reject) => { var resultHandler = lastResult => { var {value, done} = generator.next(lastResult); if (!done) { value.then(resultHandler, reject); } else { accept(value); } }; }); } function* getWeather(zip) { var temperature = yield getTemperature(zip) 72; var humidity = yield getHumidity(zip) 0.32; return {temperature, humidity}; } spawn(getWeather(zip)).then(weather => console.log(weather)); Value
  • 24. @MichaelLNorth But wait… 24 function* getWeather(zip) { var temperature = yield getTemperature(zip); var humidity = yield getHumidity(zip); return {temperature, humidity}; } async function getWeather(zip) { var temperature = await getTemperature(zip); var humidity = await getHumidity(zip); return {temperature, humidity}; } function getWeather(zip) { var temperature = getTemperature(zip); var humidity = getHumidity(zip); return {temperature, humidity}; }
  • 25. Use it today 25 @MichaelLNorth babel: { nonStandard: false, optional: [ 'es7.asyncFunctions' ] } regeneratorRuntime.wrap(function getValues$ (context) { while (1) switch (context.prev = context.next) { case 0: context.next = 2; return 1; case 2: context.next = 4; return 2; case 4: return context.abrupt("return", 31); case 5: case "end": return context.stop(); } }, ...); function *getValues() { yield 1; yield 2; return 31; }
  • 26. @MichaelLNorth Learn More 26 Netflix JavaScript Talks - Version 7: The Evolution of JavaScript