如何使包装在函数内的函数代表父函数返回

时间:2018-08-23 09:39:55

标签: javascript

我在函数内部有一个函数。我希望父函数返回一个更新的对象(循环结束后),当前,它返回undefined,因为父函数什么都不返回,只有子函数function(items)返回数据。如何使父函数返回更新的return_data?非常感谢。

reportParser: (report) => {
    const return_data = {
        "click": "[id^='AUTOGENBOOKMARK_75_'].style_12",
        "clicker": "[id^='AUTOGENBOOKMARK_74_'].style_12",
        "openning": ".style_25 > .style_24",
        "openner": ".style_25 > .style_24",
        "name": "[id^='AUTOGENBOOKMARK_7_'].style_12",
        "subject": "[id^='AUTOGENBOOKMARK_9_'].style_12",
        "audience": "[id^='AUTOGENBOOKMARK_11_'].style_12",
        "sent_mails": "[id^='AUTOGENBOOKMARK_20_'].style_12",
        "send_date": "[id^='AUTOGENBOOKMARK_32_'].style_12",
        "cancel_subscription_click": ".style_25 > .style_24",
        "cancel_subscription_clicker": ".style_25 > .style_24"
    };
    let remaining_keys = Object.keys(return_data).length;
    for (let key in return_data) {
        if (return_data.hasOwnProperty(key)) {
            html2json.parse(report, function () {
                return this.map(return_data[key], function ($item) {
                    return $item.text();
                });
            }).done(function (items) {
                if (key === "click" || key === "clicker" || key === "sent_mails") {
                    items[0] = items[0].replace(/,/g, "");
                    return_data[key] = parseInt(items[0]);
                } else if (key === "openning") {
                    items[items.length - 2] = items[items.length - 2].replace(/,/g, "");
                    return_data[key] = parseInt(items[items.length - 2]);
                } else if (key === "openner") {
                    items[items.length - 3] = items[items.length - 3].replace(/,/g, "");
                    return_data[key] = parseInt(items[items.length - 3]);
                } else if (key === "cancel_subscription_click") {
                    return_data[key] = parseInt(items[13]) + parseInt(items[18]) + parseInt(items[23]);
                } else if (key === "cancel_subscription_clicker") {
                    return_data[key] = parseInt(items[11]) + parseInt(items[16]) + parseInt(items[21]);
                } else {
                    return_data[key] = items[0];
                }
                remaining_keys--;
                if (remaining_keys === 0) {
                    return_data["click"] -= return_data["cancel_subscription_click"];
                    return_data["clicker"] -= return_data["cancel_subscription_clicker"];
                    delete return_data.cancel_subscription_click;
                    delete return_data.cancel_subscription_clicker;
                    logger.debug(return_data);
                    return return_data;
                }
            }, function (err) {
                // Handle error
            });
        }
    }
}

执行将是功能->对象初始化->等待循环更新对象->返回对象

2 个答案:

答案 0 :(得分:2)

您可以使用回调函数或将此函数写为promise。 回调是一个函数,您将在处理完数据后将其传递给函数以执行。

对于回调函数: https://developer.mozilla.org/en-US/docs/Glossary/Callback_function

reportParser = (report, callback) => { 
   //... process data
   html2json.parse(report, function() {
      //...
   }).done(function(items) {
      //after you have done process and get return_data, use callback
      callback(return_data);
   })
}

因此,当您使用reportParser时:

reportParser(report, function(return_data) { 
     //whatever you want to do with return_data
})

对于诺言: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

reportParser = (report) => {
    return new Promise(function(resolve, reject) {
        //... process data
        html2json.parse(report, function() {
        //...
        }).done(function(items) {
            //after you have done process and get return_data, use callback
             resolve(return_data);
        })
    })
}

当您使用reportParser函数时:

reportParse(report).then(return_data => { 
    //whatever you want to do with return_data
})

答案 1 :(得分:0)

似乎[featureA based on xa, ya, za; featureB based on xa, ya, za]返回一个Promise,因此在您的情况下,您还必须在父函数中返回一个Promise

html2json.parse

比起您可以正常使用reportParser: (report) => { const return_data = { "click": "[id^='AUTOGENBOOKMARK_75_'].style_12", "clicker": "[id^='AUTOGENBOOKMARK_74_'].style_12", "openning": ".style_25 > .style_24", "openner": ".style_25 > .style_24", "name": "[id^='AUTOGENBOOKMARK_7_'].style_12", "subject": "[id^='AUTOGENBOOKMARK_9_'].style_12", "audience": "[id^='AUTOGENBOOKMARK_11_'].style_12", "sent_mails": "[id^='AUTOGENBOOKMARK_20_'].style_12", "send_date": "[id^='AUTOGENBOOKMARK_32_'].style_12", "cancel_subscription_click": ".style_25 > .style_24", "cancel_subscription_clicker": ".style_25 > .style_24" }; let remaining_keys = Object.keys(return_data).length; /* here return a Promise */ return new Promise((resolve, reject) => { for (let key in return_data) { if (return_data.hasOwnProperty(key)) { html2json.parse(report, function () { return this.map(return_data[key], function ($item) { return $item.text(); }); }).done(function (items) { if (key === "click" || key === "clicker" || key === "sent_mails") { items[0] = items[0].replace(/,/g, ""); return_data[key] = parseInt(items[0]); } else if (key === "openning") { items[items.length - 2] = items[items.length - 2].replace(/,/g, ""); return_data[key] = parseInt(items[items.length - 2]); } else if (key === "openner") { items[items.length - 3] = items[items.length - 3].replace(/,/g, ""); return_data[key] = parseInt(items[items.length - 3]); } else if (key === "cancel_subscription_click") { return_data[key] = parseInt(items[13]) + parseInt(items[18]) + parseInt(items[23]); } else if (key === "cancel_subscription_clicker") { return_data[key] = parseInt(items[11]) + parseInt(items[16]) + parseInt(items[21]); } else { return_data[key] = items[0]; } remaining_keys--; if (remaining_keys === 0) { return_data["click"] -= return_data["cancel_subscription_click"]; return_data["clicker"] -= return_data["cancel_subscription_clicker"]; delete return_data.cancel_subscription_click; delete return_data.cancel_subscription_clicker; logger.debug(return_data); /* RESOLVE THE PROMISE */ resolve(return_data); return return_data; } }, function (err) { // Handle error /* REJECT THE PROMISE ON ERROR */ reject(err); }); } } }); }

的功能

Promise