2

im tring to push the return value of the resolve to a variable catWithItems which is outside the resolve. inside the resolve the catWithItems works as expected but when i console log catWithItems outside the loop it returns an empty array.

function categoriesSearch(req, res, next) {
    let categories = req.batch_categories;
    let catWithItems = [];
    _.forEach(categories, (category) => {
        return new Promise(resolve => {
            pos.categoriesSearch(req.tenant, category.id)
            .then(item => {
                if(item) category.items = item[0];
                return category;
            })
            .then(category => {
                catWithItems.push(category);
                console.log(catWithItems); //this is works inside here
                return resolve(catWithItems);
            });
        });
    });
    console.log(catWithItems); //doesn't work returns empty array
    res.json({categoryWithItems: catWithItems });
}

this is the pos.categoriesSearch module. it makes a api call to square.(this works as expected)

function categoriesSearch(tenant, category) {
    let search_items_url = ${tenant.square.api.v2}/catalog/search,
        apiKey = tenant.square.api.key,
        payload = {
            "object_types": ["ITEM"],
            "query": {
                "prefix_query": {
                    "attribute_name": "category_id",
                    "attribute_prefix": category
                }
            },
            "search_max_page_limit": 1
        },
        conf = config(search_items_url, apiKey, payload);
        return request.postAsync(conf)
        .then(items => {
            return items.body.objects;
        });
}
David Ramirez
  • 207
  • 5
  • 18
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – tkausl Oct 17 '18 at 18:25
  • Just a guess, having a method say.. `foo()` which listens to `categoriesSearch(...).then((data) => res.json({categoryWithItems: data });)`. So no need for `res.json()` in categoriesSearch() – Ashish Ranjan Oct 17 '18 at 18:45

2 Answers2

2

Your not handling promises right. Try it this way.

function categoriesSearch(req, res, next) {
    let categories = req.batch_categories;
    let promiseArray = []; // create an array to throw your promises in
    let catWithItems = [];
    categories.map((category) => {
        let promise = new Promise(resolve => {
            pos.categoriesSearch(req.tenant, category.id)
            .then(item => {
                if(item) category.items = item[0];
                return category;
            })
            .then(category => {
                catWithItems.push(category);
                console.log(catWithItems); //this is works inside here
                return resolve(catWithItems);
            });
        });
        promiseArray.push(promise) // add promises to array
    });
    // resolve all promises in parallel
    Promise.all(promiseArray).then((resolved) => {
       console.log(resolved);
       res.json({categoryWithItems: catWithItems });
    })
}
0

It should be much easier. Not sure if it works, but something to start with:

function categoriesSearch(req, res) {
    const categoryWithItems$ = req.batch_categories.map(category =>
        pos.categoriesSearch(req.tenant, category.id)
            .then(item => ({ ...category, items: item[0] })
    );

    Promise.all(categoryWithItems$)
        .then(categoryWithItems => res.json({ categoryWithItems });
}
tokland
  • 66,169
  • 13
  • 144
  • 170