【问题标题】:empty array outside nested resolve promise嵌套解析承诺之外的空数组
【发布时间】:2018-10-17 18:21:53
【问题描述】:

我想将解析的返回值推送到解析之外的变量catWithItems。在解析中 catWithItems 可以按预期工作,但是当我在循环外控制台登录 catWithItems 时,它会返回一个空数组。

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 });
}

这是 pos.categoriesSearch 模块。它对 square 进行 api 调用。(按预期工作)

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;
        });
}

【问题讨论】:

标签: javascript node.js foreach promise lodash


【解决方案1】:

你没有正确处理承诺。试试这个方法。

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 });
    })
}

【讨论】:

  • 是的,我没有正确处理承诺。你的榜样工作。谢谢
【解决方案2】:

应该会容易得多。不确定它是否有效,但可以开始:

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 });
}

【讨论】:

    猜你喜欢
    • 2015-09-02
    • 2021-03-12
    • 2021-01-08
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 2017-04-04
    • 2018-04-17
    • 2017-04-02
    相关资源
    最近更新 更多