57

I found an existing library that uses promises, however it doesn't use bluebird. The library functions don't come with all the extra features bluebird does like .map() or .tap(). How do I convert a "normal" or "non-bluebird" promise to a bluebird one, with all the extra features bluebird offers?

I tried wrapping the existing promise in Promise.promisify and Promise.resolve and neither seemed to work.

ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
  • Updated & found the correct answer, this wasn't particularly straightforward because I've always had a bluebird promise at the top of my promise chain. – ThomasReggi Jun 15 '15 at 18:41

4 Answers4

81

Use Promise.resolve - it will take any thenable, like a promise from some other implementation, and assimilate it into a Bluebird promise.

Keep in mind that the term "resolve" can be misleading, it does not mean the same as "fulfill" but can also follow another promise and settle to its result.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • @danfromisrael `Promise.resolve` will also take rejected promises and assimilate them just the same. – Bergi Jun 14 '17 at 12:03
  • 3
    Example: var Promise = require("bluebird"); Promise.resolve(nonBluebirdPromise).reflect() Reflect is a bluebird function only, and would not have been available otherwise – Leo Sep 29 '17 at 19:44
  • 1
    @danfromisrael "resolve" can be misleading. It doesn't mean "success". "resolving" a Promise means waiting until it either succeeds or fails. So Promise.resolve just settles/resolves to the value status of the passed promise, be it a success or an error. – Codebling Feb 06 '18 at 00:43
  • @d512 please don't copy CodeBling's comment into my answer, especially not without attribution – Bergi Feb 25 '18 at 07:41
  • @Bergi Why not? It improved the answer (it helped me anyway) and his wording seemed just fine so I left it the way it was. – d512 Feb 25 '18 at 15:59
  • 1
    Because, uh, [plagiarism](https://en.wikipedia.org/wiki/Plagiarism)? Upvoting his comments should have been enough. – Bergi Feb 25 '18 at 16:10
18

If you want to convert the promise to a bluebird promise resolve nothing and return the customPromise then you'll have access to all of bluebirds custom methods in the chain.

Promise.resolve().then(function(){
  return customPromise()
})

Or

Promise.resolve(customPromise())
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
3

Use Bluebird's Promise.method!

const Promise = require('bluebird');

const fn = async function() { return 'tapped!' };

bluebirdFn = Promise.method(fn);

bluebirdFn().tap(console.log) // tapped!
fn().tap(console.log) // error
richardpringle
  • 796
  • 5
  • 22
2

Using to-bluebird:

const toBluebird = require("to-bluebird");

const es6Promise = new Promise(resolve => resolve("Hello World!")); // Regular native promise.
const bluebirdPromise = toBluebird(es6Promise); // Bluebird promise.

Native alternative:

In ECMAScript:

import {resolve as toBluebird} from "bluebird"

In CommonJS:

const {resolve: toBluebird} = require("bluebird")

Usage:

const regularPromise = new Promise((resolve) => {
    resolve("Hello World!") // Resolve with "Hello World!"
})

const bluebirdPromise = toBluebird(regularPromise) // Convert to Bluebird promise

bluebirdPromise.then(val => console.log(val)) // Will log "Hello World!"
Richie Bendall
  • 7,738
  • 4
  • 38
  • 58