Jake T.
Jake T.

Reputation: 4378

Can't chain my custom function that returns a Promise in cloud code

I have a function that returns a promise. I've used it in a series of promises that looks like:

function1().then
(
    function( result )
    {
        return functionThatIsFailing();
    }
).then
(
    function( result )
    {
        response.success("It worked!");
    },
    function( error )
    {
        response.error("There was an error: " + error.message);
    }
);

But if I change things to the following, I get "cannot call method 'then' of undefined at..." as an error message.

function1().then
(
    function( result )
    {
        return functionThatIsFailing().then
        (
            function( result )
            {
                response.success("It Worked!");
            },
            function( error )
            {
                response.error("There was an error: " + error.message);
            }
        );
    }
);

Here is the custom function in question. It is sending a Twilio text message. The function itself works. I'm even receiving the text when the function fails.

function sendSMSWrapper(to, from, body)
{
    var promise = new Parse.Promise();
    twilioClient.sendSms
    ({
        to: to,
        from: from,
        body: body
    }, function(err, responseData)
    {
        if(err)
        {
            promise.reject(err);
        }
        else
        {
            promise.resolve(responseData);
        }
    });
}

edit - I've updated my function, trying a couple different things to make sure it is returning a promise, as per Bergi's suggestion, but I'm still getting the same "cannot call method 'then' of undefined..." error message. I'm opening this back up. Let me know if you have a suggestion as to how this function should be properly written!

My first attempt at fixing things:

function sendSMSWrapper(to, from, body)
{
    var promise = new Parse.Promise();
    twilioClient.sendSms
    ({
        to: to,
        from: from,
        body: body
    }, function(err, responseData)
    {
        if(err)
        {
            promise.reject(err);
            return promise;
        }
        else
        {
            promise.resolve(responseData);
            return promise;
        }
    });
}

That didn't work so I tried this:

    function sendSMSWrapper(to, from, body)
    {
        var promise = new Parse.Promise();
        twilioClient.sendSms
        ({
            to: to,
            from: from,
            body: body
        }, function(err, responseData)
        {
            if(err)
            {
                promise.reject(err);
            }
            else
            {
                promise.resolve(responseData);
            }
        });
        return promise;
    }

My last hacky method I tried:

function sendSMSWrapper(to, from, body)
{
    var promise = new Parse.Promise();
    var promise2 = new Parse.Promise();
    return twilioClient.sendSms
    ({
        to: to,
        from: from,
        body: body
    }, function(err, responseData)
    {
        if(err)
        {
            promise.reject(err);
        }
        else
        {
            promise.resolve(responseData);
        }
    });
    return Parse.Promise.when( promise ).then
    (
        function( result )
        {
            console.log(result);
            promsie2.resolve(result);
            return promise2;
        },
        function( error )
        {
            console.log(error);
            promise2.reject(error);
            return promise2;
        }
    )
}

Upvotes: 0

Views: 134

Answers (1)

Bergi
Bergi

Reputation: 664297

Apparently the functionThatIsFailing() does not return a promise. And indeed, your sendSMSWrapper is missing a return statement (even if the promise is created).

Upvotes: 1

Related Questions