当我将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);
});
});
}
答案 0 :(得分:0)
您未传递resolve
和reject
功能,因此resolve(soapResponse);
无效。
将它们添加到参数列表中并将它们传递给handleResponse
函数。
以下是使用currying的示例:
var handleResponse = (resolve, reject) => (error, soapResponse) => { ... }
client[config.webMethodName](soapBody(number), handleResponse(resolve, reject));