18
DO YOU PROMISE? / Jungkee Song @jungkees Documented using reveal.js

Do you Promise?

Embed Size (px)

DESCRIPTION

This slide has been presented in the font-end developer community seminar provided by naver.

Citation preview

Page 1: Do you Promise?

DO YOU PROMISE? / Jungkee Song @jungkees

Documented using reveal.js

Page 2: Do you Promise?

JUNGKEE SONGTwitter: @jungkees

Google+: +JungkeeSong

W3C HTML5 KOREAN INTEREST GROUPJoin

Page 3: Do you Promise?

IN THE SPOTLIGHT

Page 4: Do you Promise?

CONTENTJavaScript Runtime ModelEvents and CallbacksHello PromisesPromises guaranteeIs it ready for you?Promises the real thingPractises and ReferencesWeb standards Promise#extendthewebforward

Page 5: Do you Promise?

JAVASCRIPT RUNTIME MODELA downside

So, never block it

Run-to-completion and Event loop

“If a message takes too long to complete, theweb application is unable to process userinteractions like click or scroll.”

“A good practice to follow is to make messageprocessing short and if possible cut down onemessage into several messages.”

Page 6: Do you Promise?

EVENTSUser gesture

Click, Scroll, Touch...Resource loading

, ...Handler must be set before an event is dispatched

DOMContentLoaded load

CALLBACKS , , , basically all the

platform APIs.Callbacks in the APIs are not consistentHard to follow the flow when a callback ties with othercallbacks

IndexedDB Geolocation XMLHttpRequest

Page 7: Do you Promise?

HELLO PROMISESFrom W3C TAG Promise Guide

“A promise is an object that represents theeventual result of a single asynchronous

operation. They can be returned fromasynchronous functions, thus allowing

consumers to not only queue up callbacks to becalled when the operation succeeds or fails, butalso to manipulate the returned promise object,

opening up a variety of possibilities.”

Page 8: Do you Promise?

HELLO PROMISESConstruct a Promise

var promise = new Promise(resolver);

function resolver(resolve, reject) { // Do your task no here if ( /* things are done OK */ ) { resolve(result); } else { reject(error); }}

Use the Promise

promise.then(onFulfilled, onRejected);

function onFulfilled(result) { console.log("succeeded: " + result);}

function onRejected(error) { console.error(error);}

Page 9: Do you Promise?

HELLO PROMISESRejection handling

promise.then(undefined, function(error) { console.error(error);});

Catch - An idiomatic shorthand

promise.catch(function(error) { console.error(error);});

Page 11: Do you Promise?

PROMISES GUARANTEEOnly one of onFulfilled or onRejected will be calledonFulfilled is called with a single fulfillment valueonRejected is called with a single rejection reasonIf the promise is already settled, the hanlder will still be calledonce you attach themThe handler will always be called asynchronously

Page 12: Do you Promise?

IS IT READY FOR YOU?Firefox 29

Chrome 32 (Partial) / 33 (Full)Opera 20IE?

Yup! You can rock it with a

“Promise them that thanks to promises will be shipping in Firefox 29. :-)” -Anne van Kesteren

@nikhilcutshort

polyfill

Page 13: Do you Promise?

NOW LET'S ADDRESS A REAL ISSUEDeep nested callbacks

db.open(... fn() { db.collection(.... fn() { db.query(...... fn() { xhr.send(); xhr.onload = fn() { }; }); });});

Page 14: Do you Promise?

PROMISES THE REAL THINGChaining - Transforming values

Promise.resolve(1).then(function(value) { console.log(value); // 1 return value + 100;}).then(function(value) { console.log("Yeah, this is just " + value); // Yeah, this is just 101});

Chaining - Queuing async actions

Promise.resolve(someResult).then(function(result) { return aPromiseDoingNextTask(result);}).then(function(result) { return aYetAnotherPromiseDoingMoreTask(result);}).then(function(result) { console.log("Alright! Thanks. I got ", result);}).catch(function(error) { console.error("Darn! There was something wrong in the chain.");});

Error handling in the chain: Promise rejections skip forward tothe next "then" with a rejection callback!

Page 15: Do you Promise?

PROMISES THE REAL THINGWrite your chain of actions idiomatically!

Promise.resolve(someResult).then(function(result) { return aPromiseDoingNextTask(result);}).catch(function(error) { return retryTheFailedThingOnceMore();}).then(function(result) { return aYetAnotherPromiseDoingMoreTask(result);}, function(error) { return Promise.reject(error);}).then(function(result) { console.log("Alright! Thanks. I got ", result);}).catch(function(error) { console.error("Darn! There was something wrong in the chain.");});

Page 18: Do you Promise?

#EXTENDTHEWEBFORWARD“Promises are one of the best examples of the extensible webprinciples, where collaborative developer work informs web

standards.” - Domenic Denicola (Co-editor of Promises/A+,Member of W3C TAG)