can-zone
new Zone()
Creates a new Zone with no additional overrides. Can then call zone.run to call a function within the Zone.
var Zone = require("can-zone");
var zone = new Zone();
zone.run(function(){
return "hello world";
}).then(function(data){
data.result // -> "hello world"
});
new Zone(zoneSpec)
Create a new Zone using the provided ZoneSpec to configure the Zone. The following examples configures a Zone that will time out after 5 seconds.
var Zone = require("can-zone");
var timeoutSpec = function(){
var timeoutId;
return {
created: function(){
timeoutId = setTimeout(function(){
Zone.error(new Error("This took too long!"));
}, 5000);
},
ended: function(){
clearTimeout(timeoutId);
}
};
};
var zone = new Zone(timeoutSpec);
Parameters
- zoneSpec
{ZoneSpec|makeZoneSpec(data)}:A ZoneSpec object or a function that returns a ZoneSpec object.
These two are equivalent:
new Zone({ created: function(){ } }); new Zone(function(){ return { created: function(){ } }; });The latter form is useful so that you have a closure specific to that Zone.
Use
can-zone is a library that aids in tracking asynchronous calls in your application. To create a new Zone call it's constructor function with new:
var zone = new Zone();
This gives you a Zone from which you can run code using zone.run:
zone.run(function(){
setTimeout(function(){
}, 500);
})
then(function(){
});
The function you provide to run will be run within the Zone. This means any calls to asynchronous functions (in this example setTimeout) will be waited on.