Maybe you also want to prevent callback to execute twice if the timer
triggers and the remote service sends a response after the time limit.
function safeCallToSomeRemoteService(callback) {
var done = callback;
var timer = setTimeout(function() {
var temp = done;
done = function () {};
temp(new Error('Remote service timed out!'));
}, 3000);
unsafeCallToSomeRemoteService(function(err, data){
clearTimeout(timer);
done(err, data);
});
}
2014-04-17 2:06 GMT+02:00 Ryan Graham <[email protected]>:
>
> On Wed, Apr 16, 2014 at 11:17 AM, Alexey Petrushin
> <[email protected]> wrote:
>>
>> Is it possible to do something like this?
>>
>> function safeCallToSomeRemoteService(callback){
>> var remoteServiceResponded = false
>> unsafeCallToSomeRemoteService(function(err, data){
>> remoteServiceResponded = true
>> callback(err, data)
>> })
>>
>> setTimeout(function(){
>> if(!remoteServiceResponded){
>> somehow kill callback waiting for remote service
>> callback(new Error("Remote service hanged!"))
>> }
>> }, 3000)
>> }
>>
>> Or, if it's not possible, is it safe to just ignore such hanged callbacks,
>> call callback on timeout and move on (while leaving hanging callback
>> active)?
>
>
> Ignoring whether there are better options available through the API you are
> using, the generic timeout code would look something like this:
>
> function safeCallToSomeRemoteService(callback) {
> var timer = setTimeout(function() {
> callback(new Error('Remote service timed out!'));
> }, 3000);
>
> unsafeCallToSomeRemoteService(function(err, data){
> clearTimeout(timer);
> callback(err, data);
> });
> }
>
> ~Ryan
>
> --
> http://twitter.com/rmgraham
>
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.