0

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;
        }
    )
}
Jake T.
  • 4,308
  • 2
  • 20
  • 48

1 Answers1

1

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

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • hmph. I wonder why it works regardless? That's interesting. I guess I've been lucky, because I've been using that code in a production app without (known) issue.. oops. This is the first time I've used a .then() afterwards, I've always used it in chains where I return it then call .then() on the previous .then(). – Jake T. Oct 22 '15 at 03:18
  • Well, if the function did not return the promise that it created, then the chain (your first snippet) does not fail, but it does not wait for anything (it just carries on with `undefined` *immediately*). You should have noticed if you had actually used the `result` parameter. – Bergi Oct 22 '15 at 03:56
  • Bergi, I had to open this back up. I showed a couple things that I tried but didn't work in an edit. Mind taking another look and seeing if I'm doing something else stupid here? – Jake T. Oct 22 '15 at 17:35
  • Your second try (`function sendSMSWrapper(to, from, body) { var promise = new Parse.Promise(); twilioClient.sendSms(…, …); return promise; }`) should have been it. [This is how it works](https://parse.com/docs/js/guide#promises-creating-async-methods). There must be something else if it still threw an exception. – Bergi Oct 22 '15 at 20:16
  • I just do function sendSMSWrapper... The example does var delay = function(millis). I'll try reformatting so that sendSMS is a var = function(to, from, body) instead. – Jake T. Oct 22 '15 at 20:24
  • @JakeT.: Will make [zero difference](http://stackoverflow.com/q/336859/1048572). Your declaration is actually superior. – Bergi Oct 22 '15 at 20:26
  • 1
    I kept the declaration the same, and it turns out that second one works. I think I forgot to add in my first try that I had return twilio.... with the return promise inside, and I must have left the return twilio in there even though I had return promise later, so it wasn't actually returning a promise. Programming has a way of making me feel so godlike sometimes, and so stupid others... Thanks for your help. – Jake T. Oct 22 '15 at 20:56