kriszyp / node-promise

Promise utilities for Node
397 stars 42 forks source link

How to handle responses that come in after timeout + cancel? #8

Closed ifavo closed 2 years ago

ifavo commented 11 years ago

Hi there!

I have a promise that has a timeout configured but sometimes there still will be a late response.

When this late response comes in, it throws an error which I can not catch.

throw new Error("This deferred has already been resolved");

How should I handle this situation?

This is how I setup the timeout:

var p = rpc.call("run", request.payload);
p.timeout(1000);
p.then(

    // success
    function(data){
        callback(data);
    },

    // error
    function(data){
        sendError(data, callback);
    }
);

And this is the other side:

rpc.expose("run", function(request){
    var p = new Promise();

    try {
        setTimeout(function () {
            p.callback(request);
        }, 5000);
    }
    catch (e) {
        p.errback(e);
    }

    return p;
});

Thanks, Mario