Skip to content

Perform operations in your promise chain without breaking it

License

Notifications You must be signed in to change notification settings

delta62/promise-do

Repository files navigation

Build Status npm version

promise-do

Perform operations in your promise chain without breaking it

Usage

Once you've imported promise-do, you can call it from any promise object. It behaves similar to Promise.prototype.then.

A simple use case would be adding debugging statements into a chain of promises:

require('promise-do');

Promise.resolve(42)
  .do(val => console.log(`The value is ${val}`))
  .then(val => assert.equal(val, 42));

This would not have worked with .then, since the promise value (42) would have been lost when console.log returned.

Promise.resolve(42)
  .then(val => console.log(`The value is ${val}`))
  .then(val => assert.equal(val, 42)); // ERROR: val === undefined

You can wait for promises returned in the do function also, just like then:

Promise.resolve(42)
  .do(val => new Promise((resolve, reject) => {
    setTimeout(() => resolve(val + 100), 1000);
  }))
  .then(val => assert.equal(val, 142));

License

MIT

About

Perform operations in your promise chain without breaking it

Resources

License

Stars

Watchers

Forks

Packages

No packages published