forEach未处理的拒绝错误

时间:2017-09-15 11:27:04

标签: javascript node.js promise bluebird

我尝试使用forEach迭代数组时处理拒绝。 这就是问题所在:

module.exports = function (alert, level, subscribers, tagSubscribers, message_type) {
    return decrypt(config.slack_bot_token)
        .then(function (auth_token) {
            console.log('Dispatch got the token from kms');
            subscribers.forEach(function(subscriber){
                return makePostCall(alert, level, subscriber, tagSubscribers, message_type, auth_token);
            });
        })
        .catch(function (error) {
            console.error('Error during dispatch: ' + error);
            throw new Error(error);
        });
};

makePostCall返回resolvereject,并沿着这些方向查看...

function makePostCall(alert, level, subscriber, tagSubscribers, message_type, auth_token) {
    return new Promise(function (resolve, reject) {
        const headers = ...
        const url = ...
        var payload = ...

        request.post({
            url: url,
            headers: headers,
            form: payload
        }, function (error, response, body) {
            if (!error && response.statusCode === 200 && JSON.parse(body).ok === true) {
                console.log('successfully published to Slack');
                return resolve();
            }
            else if (!error && response.statusCode === 200 && JSON.parse(body).ok === false) {
                console.error('Status is 200 but ok is false: ' + JSON.stringify(body));
                return reject(new Error(JSON.stringify(body)));
            }
            else {
                console.error('Status other than 200: ' + JSON.stringify(error));
                return reject(new Error(JSON.stringify(error)));
            }
        });
    });
}

现在,在运行单元测试时,我收到了很多未处理的拒绝错误

Unhandled rejection Error: {"status":404,"statusCode":404}
    at Request._callback (/Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/lib/dispatchAlert.js:63:31)
    at self.callback (/Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/node_modules/request/request.js:188:22)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at Request.onRequestError (/Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/node_modules/request/request.js:884:8)
    at emitOne (events.js:96:13)
    at OverriddenClientRequest.emit (events.js:188:7)
    at /Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/node_modules/nock/lib/request_overrider.js:212:11
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

作为参考,我使用Bluebird for Promises。 我对Node& amp;承诺。试图使用async / await来避免这种混乱,但Node6.10还是不支持。

1 个答案:

答案 0 :(得分:1)

实际上,没有人听取承诺;他们只是被扔掉了。在Promise.all()中包含承诺可以解决您的问题。

module.exports = function (alert, level, subscribers, tagSubscribers, message_type) {
    return decrypt(config.slack_bot_token)
        .then(function (auth_token) {
            console.log('Dispatch got the token from kms');
            return Promise.all(subscribers.map(function(subscriber){
                return makePostCall(alert, level, subscriber, tagSubscribers, message_type, auth_token);
            }));
        })
        .catch(function (error) {
            console.error('Error during dispatch: ' + error);
            throw new Error(error);
        });
};