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

Made with Slides.com