【问题标题】:having issue with async.each , callback is geting call before completing taskasync.each 有问题,回调在完成任务之前被调用
【发布时间】:2017-07-27 08:58:11
【问题描述】:

我遇到了 async.each 的问题,回调在完成任务之前被调用,我有下面的代码,输出为 调用 reprice 7 在调用 reprice 6 之前执行,6 是我在 async.each 回调中构建我需要的数组的部分

called reprice :
called reprice : 2
called reprice : 3
**called reprice : 7  []**
no chnages has been made 
called reprice : 6  { status: 200, msg: 'recived optimal price 4 ', data: 17.75 }
called reprice : 6  { status: 200, msg: 'recived optimal price 4 ', data: 20.99 }

这是代码

async.forEach(records, function(result, callback) {
    switch (result.Pricing) {
        case  "PF" : // current scenrio
            var pfObject = _.filter(rules, {'sub_title': 'PF'});
            checkRepriceFrequency(result, pfObject[0], function (response) {
                if (response.status === 200) {
                    // call check inventory rules
                    CheckInventoryRules(result, pfObject[0], function (response) {
                        console.log('called reprice : 6 ',response);
                        if (response.status === 200) {
                            var price = result.sales_price;
                            var pushData = {
                                price: price,
                                optimalPrice: response.data,
                                SKU: result.SKU,
                                _id: result._id,
                                frequency: pfObject[0].reprice_frequency
                            };
                            productArray.push(pushData);
                        }
                    });
                } else {
                    console.log('err');
                }
            });
        }
        callback(null);
    }, function(err) {
        console.log('called reprice : 7 ',productArray);
    if (!err) {
        if (productArray.length > 0) {
            console.log('To update Synch : ',productArray);
        } else {
            console.log("no chnages has been made ");
        }
    }
});

【问题讨论】:

  • 您好,谁能帮帮我?

标签: javascript angularjs node.js loops callback


【解决方案1】:

是的。所以你实现async.each 有点错误。看, async.each 确保每个对象都通过循环,但不确保同步执行。在您的示例中,您调用了 CheckInventoryRules,这似乎是在进行 AJAX 调用。

但是检查一下,您在函数末尾调用了回调(它不会等待 AJAX 完成)。这意味着在完成 AJAX 调用之前,可能已经调用了回调,表明该特定对象的处理已完成。

要修复它,您需要检查是否正在调用 AJAX 调用,是否需要在 productArray.push(pushData); 之后调用 callback(null)

一般来说,只有在任务执行完毕后才调用callback

【讨论】:

  • 谢谢@kawadhiya21,这就是问题所在,但是如果我在 productArray.push(pushData); 之后放置 callback(null); , 然后回调是在第一次推送后第一次匹配得到调用
【解决方案2】:

你已经完全错误地实现了 async.forEach。 See this.

switch 中似乎有一个异步调用,因此您必须在该异步调用中调用 callback(null)

async.forEach(records, function(result, callback) {
    switch (result.Pricing) {
        case  "PF" :
            var pfObject = _.filter(rules, {'sub_title': 'PF'});
            checkRepriceFrequency(result, pfObject[0], function (response) {
                if (response.status === 200) {
                    CheckInventoryRules(result, pfObject[0], function (response) {
                        console.log('called reprice : 6 ',response);
                        if (response.status === 200) {
                            var price = result.sales_price;
                            var pushData = {
                                price: price,
                                optimalPrice: response.data,
                                SKU: result.SKU,
                                _id: result._id,
                                frequency: pfObject[0].reprice_frequency
                            };
                            productArray.push(pushData);
                            callback(null);
                        }
                    //handle callback on else otherwise final callback will not get the event.
                    });
                } else {
/*
                    callback(null);
                    Or
                    callback(Someerror);
*/
                    console.log('err');
                }
            });
            break;
        default:
            callback(null);
        // return false;


        //break;


    }


}, function(err) {
    console.log('called reprice : 7 ',productArray);
    if (!err) {
        if (productArray.length > 0) {
            console.log('To update Synch : ',productArray);

        } else {
            console.log("no chnages has been made ");
        }
    }
});

【讨论】:

  • 谢谢@vkstack 我试过这样但是回调正在执行第一次匹配的“PF”,
  • 这是一种将默认值始终放在您的 switch 语句中的做法。但选择始终是您的。
猜你喜欢
  • 2020-10-31
  • 2016-01-26
  • 2015-09-12
  • 2019-03-31
  • 1970-01-01
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
  • 2016-08-19
相关资源
最近更新 更多