Search code examples
javascriptnode.jsecmascript-6promisees6-promise

named function does not resolve or reject promise


When i move promise's resolve/reject handling to a named function it does not work. Can someone explain why please?

works:

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);
            });
        });
    });
}

doesn't work:

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);
        });
    });
}

Solution

  • You're not passing along the resolve and reject functions, thus, resolve(soapResponse); will not work.

    Add those to the parameter list and pass them to the handleResponse function.

    Here's an example using currying:

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