SlideShare a Scribd company logo
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

More Related Content

What's hot

Behind modern concurrency primitives
Behind modern concurrency primitivesBehind modern concurrency primitives
Behind modern concurrency primitives
Bartosz 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 210
Mahmoud 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 extensions
Oleksandr 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 promises
Ankit Agarwal
 
Android getting started
Android getting startedAndroid getting started
Android getting started
Uptech
 
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
Brandon 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 platform
Lucio 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-es
MongoDB
 
Driving User Engagement with watchOS 3
Driving User Engagement with watchOS 3Driving User Engagement with watchOS 3
Driving User Engagement with watchOS 3
Kristina 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 SaltStack
Love Nyberg
 
Intelligent infrastructure with SaltStack
Intelligent infrastructure with SaltStackIntelligent infrastructure with SaltStack
Intelligent infrastructure with SaltStack
Love Nyberg
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.js
Piotr Pelczar
 
RxJS In-Depth - AngularConnect 2015
RxJS In-Depth - AngularConnect 2015RxJS In-Depth - AngularConnect 2015
RxJS In-Depth - AngularConnect 2015
Ben Lesh
 
Correcting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await MistakesCorrecting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await Mistakes
Brandon Minnick, MBA
 
FullStack Reativo com Spring WebFlux + Angular
FullStack Reativo com Spring WebFlux + AngularFullStack Reativo com Spring WebFlux + Angular
FullStack Reativo com Spring WebFlux + Angular
Loiane Groner
 
Reduxing like a pro
Reduxing like a proReduxing like a pro
Reduxing like a pro
Boris Dinkevich
 
Pipeline interface
Pipeline interfacePipeline interface
Pipeline interface
allanh0526
 

What's hot (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
 

Similar to 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 platform
HaiderAli650468
 
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 practices
Ankit 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
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
Zsolt Mészárovics
 
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 Programmers
David 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 Introduction
Dmitry Sheiko
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
Tomáš Kypta
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
Lin 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 gradle
Thierry Wasylczenko
 
The redux saga begins
The redux saga beginsThe redux saga begins
The redux saga begins
Daniel Franz
 
ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation Javascript
Ramesh 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.
Michele Giacobazzi
 
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
 
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 API
Maxim Zaks
 

Similar to 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
 

More from Mike North

Web Security: A Primer for Developers
Web Security: A Primer for DevelopersWeb Security: A Primer for Developers
Web Security: A Primer for Developers
Mike 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 Glue
Mike North
 
Anatomy of a Progressive Web App
Anatomy of a Progressive Web AppAnatomy of a Progressive Web App
Anatomy of a Progressive Web App
Mike North
 
Web and Native: Bridging the Gap
Web and Native: Bridging the GapWeb and Native: Bridging the Gap
Web and Native: Bridging the Gap
Mike North
 
The Road to Native Web Components
The Road to Native Web ComponentsThe Road to Native Web Components
The Road to Native Web Components
Mike North
 
Enemy of the state
Enemy of the stateEnemy of the state
Enemy of the state
Mike North
 
Phoenix for Rubyists - Rubyconf Brazil 2016
Phoenix for Rubyists - Rubyconf Brazil 2016Phoenix for Rubyists - Rubyconf Brazil 2016
Phoenix for Rubyists - Rubyconf Brazil 2016
Mike North
 
Phoenix for Rubyists
Phoenix for RubyistsPhoenix for Rubyists
Phoenix for Rubyists
Mike 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 Munich
Mike North
 
Delightful UX for Distributed Systems
Delightful UX for Distributed SystemsDelightful UX for Distributed Systems
Delightful UX for Distributed Systems
Mike 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.js
Mike North
 
Ember addons, served three ways
Ember addons, served three waysEmber addons, served three ways
Ember addons, served three ways
Mike 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 Apps
Mike North
 
User percieved performance
User percieved performanceUser percieved performance
User percieved performance
Mike North
 
User-percieved performance
User-percieved performanceUser-percieved performance
User-percieved performance
Mike North
 
Write Once, Run Everywhere
Write Once, Run EverywhereWrite Once, Run Everywhere
Write Once, Run Everywhere
Mike 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.js
Mike North
 
Modern Web UI - Web components
Modern Web UI - Web componentsModern Web UI - Web components
Modern Web UI - Web components
Mike North
 

More from 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
 

Recently uploaded

Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 

Recently uploaded (20)

Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 

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