Promises



Promises

0 0


sdjs-promises

Promises talk for SanDiego.js Community event Feb 2, 2016

On Github hbrysiewicz / sdjs-promises

Promises

Scenario 1

you want to go camping you don’t really care when but you know you want to go camping

Xavier

but you lent your tent to xavier and he hasn’t given it back
let lentXavierTent = Promise((resolve, reject) => {
  // ...
  resolve(tent)
  // ...
  reject(excuse)
})
here is what that looks like as a promise
lentXavierTent.then((tent) => {
  // go camping
}).catch((excuse) => {
  // buy a new tent...
})
and here is how you would then use that promise

Scenario 2

you want to go camping you don’t really care when but you know you want to go camping

Xavier

Kat

Stuart

but you lent your tent to xavier and your backpack to kat and your sleeping bag to stuart
let lentXavierTent = Promise((resolve, reject) => { ... })
let lentKatBackpack = Promise((resolve, reject) => { ... })
let lentStuartBag = Promise((resolve, reject) => { ... })
here is what those look like as promises
let promises = [lentXavierTent, lentKatBackpack, lentStuartBag]
Promise.all(promises).then((campingGear) => {
  // go camping
}).catch((excuse) => {
  // buy a new tent...
})
if any of the passed in promises rejects, the all Promise immediately rejects with the value of the promise that rejected

Scenario 3

you want to go camping you don’t really care when but you know you want to go camping

Xavier Dianna

Kat

Stuart

but you lent your tent to xavier and your backpack to kat and your sleeping bag to stuart
let lentDiannaTent = Promise((resolve, reject) => { ... })
let lentXavierTent = Promise((resolve, reject) => { 
  lentDiannaTent.then(resolve).catch(reject)
})
let lentKatBackpack = Promise((resolve, reject) => { ... })
let lentStuartBag = Promise((resolve, reject) => { ... })
here is what those look like as promises
let promises = [lentXavierTent, lentKatBackpack, lentStuartBag]
Promise.all(promises).then((campingGear) => {
  // go camping
}).catch((excuse) => {
  // buy a new tent...
})
if any of the passed in promises rejects, the all Promise immediately rejects with the value of the promise that rejected

Thanks!

Heather Brysiewicz

@caligoanimus

hbrysiewicz.com/sdjs-promises

Promises