Home Ask Login Register

Developers Planet

Your answer is one click away!

leo-er February 2016

Extend bluebird promise resolved value using bluebird .return() method

I'm trying to extend promise resolved value by using bluebird's .return() method.

Currently I'm using the following code:

doSomethingAsync()              // assume resolved value is {a: 0}
    .return({b: 1})
    .then(console.log);         // prints {b: 1}

I would like to extend the result object ({b: 1}) with doSomethingAsync resolved value, in this case {a: 0}, so I could write something like:

var extend = require('extend');

doSomethingAsync()                          // assume resolved value is {a: 0}
    .return(extend(resolvedValue , {b: 1}))
    .then(console.log);                     // should print {a: 0, b: 1}

Is it possible to access doSomethingAsync() resolved value (in that case {a: 0}) from within the .return() parameter?


Chris911 February 2016

As I mentioned in my comment this is not how .return should be used. Simply use something like this instead:

const Promise = require('bluebird');
const _ = require('lodash');

Promise.resolve({a: 1}).then(obj => _.extend(obj, {b: 2})).then(console.log);

Prints: { a: 1, b: 2 }

Post Status

Asked in February 2016
Viewed 1,732 times
Voted 7
Answered 1 times


Leave an answer

Quote of the day: live life