presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of...

27
I PROMISED MYSELF Christian Ulbrich, Zalari UG

Transcript of presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of...

Page 1: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

I PROMISED MYSELFChristian Ulbrich, Zalari UG

Page 2: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

AGENDA PROPAGANDA• Recap: Promises

• Anti-Patterns

• Common use cases

• Advanced Example - lazyCache

• Links

Page 3: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

RECAP - PROMISES

„Promises are the monad of asynchronous programming"

Page 4: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

RECAP - PROMISES

Page 5: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

RECAP - PROMISES

• Promises are an elegant concept of modeling asynchronous data flows

• Promises are not JS-specific, but can be found in other language as well

• Promises avoid the pyramid of doom aka callback hell

Page 6: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

RECAP - PROMISES

Page 7: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

RECAP - PROMISES

Page 8: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

RECAP - PROMISES• Promises are something, that can either be fulfilled or rejected

• a Promise has two main methods

• then(callback) , that gets called, when the promise is fulfilled

• catch(callback), that gets called, when the promise is rejected

Page 9: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

WHY - PROMISES

• Promises are native in ECMAScript 6 and current NodeJS

• a growing number of standard functions return Promises

• the order does not matter, i.e. attach then before or after a promise resolves / rejects

Page 10: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

PROMISES - ORDER

Page 11: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

PROMISES - ORDER

Page 12: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

PROMISES - NATIVE

Page 13: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

PROMISES - NATIVE

Page 14: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

ANTI-PATTERNS

• nested promises

• superfluous deferred

• then-callback-style

Page 15: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

ANTI-PATTERNSNESTED PROMISES

Page 16: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

ANTI-PATTERNSNESTED PROMISES

Page 17: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

ANTI-PATTERNSSUPERFLUOUS DEFERRED

Page 18: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

ANTI-PATTERNSSUPERFLUOUS DEFERRED

Page 19: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

ANTI-PATTERNSTHEN-CALLBACK STYLE

Page 20: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

ANTI-PATTERNSTHEN-CALLBACK STYLE

Page 21: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

COMMON USE CASE

• doing things in parallel

• doing things in sequence

• same interfaces for sync / async code

Page 22: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

COMMON USE CASEPARALLELISM

Page 23: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

SEQUENCE

Page 24: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

COMMON USE CASESAME INTERFACES

Page 25: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

ADVANCED EXAMPLE - LAZY CACHE

• we need to fetch data

• fetch is async

• we want to hide details of cache

• interface should always be the same

Page 26: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

LAZYCACHE

Page 27: presentation I promised myself - GitHub...RECAP - PROMISES • Promises are an elegant concept of modeling asynchronous data flows • Promises are not JS-specific, but can be found

LINKS• Promises are the monad of asynchronous

programming http://bit.ly/1pH8qxJ

• Callbacks are Imperative, Promises are functional: http://bit.ly/1hwxDEf

• Promise Anti-Patterns http://bit.ly/1bKThTD

• Promise Tutorial http://bit.ly/1fgVvY9