0

I am new to node.js and I am having hard time with Promises. I want to construct/build my result variable step by step by using Promises. I "abstracted" my code just to point out the issue better. Basically what I am trying to do is to create a resulting model of several rest api calls (those calls that can be done in parallel are called in Promise.all).

Thank you in advance.

function test() {
    var result = {}; 
    var prom1 = new Promise(function(resolve, reject) {
        resolve(addTwo(result)); 
    }).catch(err => console.log(err));
    return prom1.then(function(result) {
            promises = [];
            promises.push(new Promise(function(resolve, reject) {
                resolve(addD(result)); 
            }));
            promises.push(new Promise(function(resolve, reject) {
                resolve(addC(result));
            }));
            Promise.all(promises)
                         .then(result) 
    }).then(console.log(result)); //logging because I was testing
}

function addTwo(result) {
    result.a = "a";
    result.b = "b";
    return result;
}

function addD(result) {
    result.d = "d";
}

function addC(result) {
    result.c = "c";
}

test();

The output that expected was: { a: 'a', b: 'b', d: 'd', c: 'c' }, but instead I got: { a: 'a', b: 'b' }

I understand that if I call then() on a Promise, that I will have in that block access to the return value from the promise, but can I adjust my code somehow to "build" the result variable in the then calls with using Promise.all at some point?

3
  • You need to pass a callback to then, like .then(() => { return result }) or .then(() => { console.log(result); }). Commented Jul 28, 2019 at 20:55
  • Btw, I'm not sure what parts of the code you abstracted, but you should have the asynchronous functions addTwo, addC and addD create and return the promises themselves. Also you don't need new Promise here, you could just have used Promise.resolve(…). Commented Jul 28, 2019 at 20:57
  • @Bergi thank you for the help, it worked! I also restructed my code. Thanks again! Commented Jul 29, 2019 at 12:42

2 Answers 2

1
  1. You need to return your Promise.all(promises) so that its result is chained to the then where you have console.log(result).
  2. I believe you have an error at the line Promise.all(promises).then(result), you pass result to then but then expects a function as an argument, not an object

Consider using async/await cause it's less confusing than these Promise chains

Sign up to request clarification or add additional context in comments.

Comments

0

In a more ES8 way with the async/await syntax you could escape the .then().catch() struggle. I feel like it's easier to get how promises work this way.

async function test() {
    try {
        let result = {}
        await new Promise(function(resolve, reject) {
            resolve(addTwo(result))
        })
        let promises = []
        promises.push(new Promise(function(resolve, reject) {
            resolve(addD(result))
        }))
        promises.push(new Promise(function(resolve, reject) {
            resolve(addC(result))
        }))
        await Promise.all(promises)
        return result
    } catch (e) {
        console.error(e)
    }
}

Note that you'll have to await your test function result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.