命名函数不解析或拒绝承诺

时间:2017-02-13 10:28:34

标签: javascript node.js ecmascript-6 promise es6-promise

当我将promise的解决/拒绝处理移动到命名函数时,它不起作用。有人可以解释为什么吗?

工作的:

function getPremium(policyNumber, agentNumber) {
    return new Promise(function (resolve, reject) {
        soap.createClient(wsdl, function (error, client) {
            client.addSoapHeader(soapHeader());
            client[config.webMethodName](soapBody(number), (error, soapResponse) => {
                return resolve(soapResponse);
            });
        });
    });
}

不起作用:

var handleResponse = (error, soapResponse) => {
    return resolve(soapResponse);
}

function getPremium(policyNumber, agentNumber) {
    return new Promise(function (resolve, reject) {
        soap.createClient(wsdl, function (error, client) {
            client.addSoapHeader(soapHeader());
            client[config.webMethodName](soapBody(number), handleResponse);
        });
    });
}

1 个答案:

答案 0 :(得分:0)

您未传递resolvereject功能,因此resolve(soapResponse);无效。

将它们添加到参数列表中并将它们传递给handleResponse函数。

以下是使用currying的示例:

var handleResponse = (resolve, reject) => (error, soapResponse) => { ... }
client[config.webMethodName](soapBody(number), handleResponse(resolve, reject));