can-zone/timeout
timeout(ms)
Creates a ZoneSpec that you can use as a plugin for your Zone in order to timeout after a certain length of time (as ms
).
If the Zone times out it's run promise will be rejected with a <a href="timeout.TimeoutError.html" title="A special type of Error that also includes the number of milliseconds that were waited before timing out. The error object is included with the timeout module: var timeout = require("can-zone/timeout");
var TimeoutError = timeout.TimeoutError;
// Maybe use this to check instanceof
.">TimeoutError, a special error that also includes the number of milliseconds waited before timing out.
var Zone = require("can-zone");
var timeout = require("can-zone/timeout");
var zone = new Zone({
plugins: [ timeout(5000) ]
});
zone.run(function(){
setTimeout(function(){
}, 10000); // waiting over 5 sec
})
.catch(function(err){
// Called because we exceeded the timeout.
});
Parameters
- ms
{Number}
:The number of milliseconds to wait before timing out the Zone.
Use
The timeout zone allows you to specify a timeout for your Zone. If the Zone promise doesn't resolve before timing out, the Zone promise will be rejected by the plugin.
The timeout zone is a function that takes a timeout in milliseconds.
The Promise will reject with a special type of Error, a TimeoutError.
var Zone = require("can-zone");
var timeout = require("can-zone/timeout");
var TimeoutError = timeout.TimeoutError;
var zone = new Zone({
plugins: [
timeout(2000)
]
});
zone.run(function(){
setTimeout(function(){
}, 5000);
}).then(null, function(err){
// err.timeout -> 2000
});