1

In my Node.js app I have a function that loops over an array of URLs and get their values, once loop is done it will return the final value as a Map. Code following.

let getResults = function(urls){
    let results = new Map();
    let retrievePromises = [];
    urls.forEach(function (value, i) {
        retrievePromises.push(
            restAgent.get(value).then(function(data){
                for (let item of data.items) {
                    let name = item.filter(n => n.name === "somename"); //filter array
                    let obj = {};
                    obj.name = item.name;
                    obj.address = item.address;
                    results.set(name, obj);
                }

            }).catch(function(err){
                console.log(err);
            })
        );
    });

    Promise.all(retrievePromises).then(function(){
        return this.results;
    });
}

If then I run, console.log(getResults()); it will return undefined as it is triggered before the function returns results. How to solve this issue, and since I am new to Promises, any possible improvements to the code? (I know that forEach is not favorable)

1 Answer 1

2

You have to return the Promise. In your case you will have to return the return Promise.all

let getResults = function(urls){
    let results = new Map();
    let retrievePromises = [];
    urls.forEach(function (value, i) {
        retrievePromises.push(
            restAgent.get(value).then(function(data){
                for (let item of data.items) {
                    let name = item.filter(n => n.name === "somename"); //filter array
                    let obj = {};
                    obj.name = item.name;
                    obj.address = item.address;
                    results.set(name, obj);
                }

            }).catch(function(err){
                console.log(err);
            })
        );
    });

    return Promise.all(retrievePromises).then(function(){
        return results;
    });
}

There is no need for improvements, you code code is optimal. Good job!

To check the results you will have to do .then() when you call getResults(). If you do console.log(getResults()); you will get the promise object.

To see the result you have to do

getResults(urls).then(function(results){
    console.log(results);
})
Sign up to request clarification or add additional context in comments.

3 Comments

Also has to use then() when calling this function
Thanks for pointing that out @charlietfl I added it to my answer.
Since this was selected as correct , I assume it is working . I wasn't sure about what this in this.results point to ? I thought it would point to the function in the then in Promise.all

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.