Give the word and

keep your promises

Gilberto López

Tech lead @ La Creativería

glixius @ GitHub

glopezr@lacreativeria.com

+506 8703 1266

Javascript Context

  • Asynchronous language.
  • Callback hell.
fs.readdir(source, function (err, files) {
  if (err) {
    console.log('Error finding files: ' + err)
  } else {
    files.forEach(function (filename, fileIndex) {
      console.log(filename)
      gm(source + filename).size(function (err, values) {
        if (err) {
          console.log('Error identifying file size: ' + err)
        } else {
          console.log(filename + ' : ' + values)
          aspect = (values.width / values.height)
          widths.forEach(function (width, widthIndex) {
            height = Math.round(width / aspect)
            console.log('resizing ' + filename + 'to ' + height + 'x' + height)
            this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
              if (err) console.log('Error writing file: ' + err)
            })
          }.bind(this))
        }
      })
    })
  }
})

🧔🏽

👨🏻‍🍳

🥘

Gilberto

Rémulo

Arroz de maíz

📲

Promise

MDN

A Promise is an object representing the eventual completion or failure of an asynchronous operation.

Gilberto

Delivery ticket.

👦🏻

👩🏽

🍗

Kevin

Cajera

Pollo frito

📟

Dispositivo

States

Pending

Fulfilled

👌🏽

Rejected

👎🏽

Settled

😌

Methods

Simple

then

catch

finally

Complex

all

race

Create

resolve

reject

Code time!!

execution Flow control

execution Flow control

1

2

3

Chaining

Error propagation

Nesting

Summary

  • Promises are like delivery tickets.
  • Promises always return values asynchronously.
  • You can create composed promises with parallel methods.
  • Use resolve and reject methods for controlling resolution flow.
  • Get meaningful code by chaining promises.
  • Take advantage of promises' error propagation.
  • Use nesting to control what your promise will return.

not enough?

  • Creating promises.
  • Using promises with async/await (ES2017).

Keep your promises and

BE better developers

Give the word and keep your promises

By Gilberto López Rodríguez

Give the word and keep your promises

Promises are the new basic async object in javascript, but most of the time people don't understand how powerful they can be. This presentation is intended to open the promise flow world to javascript developers.

  • 29