embracing async with deferreds and promises

25
Embracing Async with Deferreds & promises Sebastian Porto @sebasporto

Upload: sebasporto

Post on 17-Jul-2015

1.044 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Embracing Async with Deferreds and Promises

Embracing Async with Deferreds & promises

Sebastian Porto@sebasporto

Page 2: Embracing Async with Deferreds and Promises

Let’s do greeting card

Page 3: Embracing Async with Deferreds and Promises

Animation 1

Animation 2

Music 1

Music 2

Next

Page 4: Embracing Async with Deferreds and Promises

Load animation !

Load music !

Show !

Select !animation !

& !music !

Knowing everything at the start

Page 5: Embracing Async with Deferreds and Promises

var assetsToLoad=2;loader.on(‘load’, function(){

assetsToLoad --;if(assetsToLoad===0) show();

});loader.load(anim);loader.load(music);

We can count

Page 6: Embracing Async with Deferreds and Promises

Use a lib (e.g. Async)

async.parallel([

loadAnim,loadMusic,

], show);

Check Async by the way

Page 7: Embracing Async with Deferreds and Promises

Async is nice and clean but…

Page 8: Embracing Async with Deferreds and Promises

Animation 1

Animation 2

Music 1

Music 2

Page 9: Embracing Async with Deferreds and Promises

Load animation !

Load music !

Show !

Select !animation !

Select !music !

Wait? !

Incomplete information

Page 10: Embracing Async with Deferreds and Promises

Load animation !

Load music !

Show !

Select !animation !

Incomplete information

Select !music !

Page 11: Embracing Async with Deferreds and Promises

function onVideoLoaded(){checkIfAllLoaded();

}function checkIfAllLoaded(){

if(videoLoaded && musicLoaded && … ) show();}

We can count again or"use conditionals

Page 12: Embracing Async with Deferreds and Promises

Becomes hard to maintain very quickly

Page 13: Embracing Async with Deferreds and Promises

Deferreds to the rescue

var def = $.Deferred();def.done(function(val){

//… do something});//… laterdef.resolve(val);

Page 14: Embracing Async with Deferreds and Promises

jQuery Deferreds"Underscore deferreds"

PromisedIO"… many others"

Page 15: Embracing Async with Deferreds and Promises

They can be aggregated

var def1 = $.Deferred();var def2 = $.Deferred();$.when(def1, def2).done(function(one, two){

//… do something with one and two;});//… laterdef1.resolve(val);//… even laterdef2.resolve(val);

Page 16: Embracing Async with Deferreds and Promises

Load animation !

Load music !

Show !

Select !animation !

Select !music !

To do this

Page 17: Embracing Async with Deferreds and Promises

var animDef = $.Deferred();var musicDef = $.Deferred();$.when(animDef, musicDef).done(function(){

show(); });//when the music is loadedmusicDef.resolve();//when the animation is loadedanimDef.resolve();

Page 18: Embracing Async with Deferreds and Promises

Load animation !

Load music !

Show !

Select !animation !

Select !music !

Already resolved?

Page 19: Embracing Async with Deferreds and Promises

var animDef = $.Deferred();var musicDef = $.Deferred();//…latermusicDef.resolve();//…even later$.when(animDef, musicDef).done(function(){

show(); });//No problem, still triggers, you cannot do that with event listeners!

Page 20: Embracing Async with Deferreds and Promises

Fail and reject

var def = $.Deferred();def

.done(function(result){ //do something}).fail(function(){ //fallback});

//…later, something bad happeneddef.reject();

Page 21: Embracing Async with Deferreds and Promises

Promises

Page 22: Embracing Async with Deferreds and Promises

Promises

Loaderfunction load(){

def = $.Deferred();return def.promise();

}//..laterfunction onLoad(){

def.resolve();}

Callerpromise = loader.load();promise.done(function(){

..something});//Cannot resolve here://promise.resolve() not possible

Page 23: Embracing Async with Deferreds and Promises

Combine them to create bigger promises

var promise1 = $.when(animDef, musDef);var promise2 = $.when(msgDef, bgDef);$.when(promise1, promise2).done(function(){

//… do something with anim, music message and background});

Page 24: Embracing Async with Deferreds and Promises

A !

C !

Result !

B !

F !

D !

E !

G !

H !

Page 25: Embracing Async with Deferreds and Promises