0

I use promises in my website (still learning) and I would like to know if there is a difference between this:

    return promise
            .then(ctxTransport.getTransportById(idTran, transport))
            .then(checkLocking)
            .fail(somethingWrong);

and this:

    return promise
        .then(function () { return ctxTransport.getTransportById(idTran, transport); })
        .then(function () { return checkLocking(); })
        .fail(somethingWrong);

With the first implementation sometimes I got errors.

var getTransportById = function (transportId, transportObservable, forceRemote) {
    // Input: transportId: the id of the transport to retrieve
    // Input: forceRemote: boolean to force the fetch from server
    // Output: transportObservable: an observable filled with the transport

    ...

    return manager.executeQuery(query)
        .then(querySucceeded)
        .fail(queryFailed);

    function querySucceeded(data) {
        transportObservable(data.results[0]);
    }
};

function checkLocking() {
     var now = new Date();
     transport().lockedById(5);
     transport().lockedTime(now);
     return ctxTransport.saveChanges(SILENTSAVE);
 }

 function somethingWrong(error) {
     var msg = 'Error retreiving data. ' + error.message;
     logError(msg, error);
     throw error;
 }

Thanks.

Bronzato
  • 9,438
  • 29
  • 120
  • 212

1 Answers1

0

When passing functions in the promise chain, you're supposed to pass the function names without arguments or the (), or as in the second case, anonymous functions. This is because Q will call it for you with the result/return value of the previous promise resolution.

Therefore, .then(ctxTransport.getTransportById(idTran, transport)) is semantically incorrect, since you're not passing a function, but the return value of ctxTransport.getTransportById.

abject_error
  • 2,858
  • 1
  • 19
  • 23