DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
  • Ecosystem
    • can-construct-super
    • can-define-stream
    • can-fixture
    • can-fixture-socket
    • can-jquery
    • can-stache-converters
    • can-stream
    • can-vdom
    • can-view-import
    • can-zone
      • types
        • ZoneSpec
        • makeZoneSpec
      • static
        • current
        • error
        • ignore
        • waitFor
      • prototype
        • addWait
        • data
        • removeWait
        • run
      • plugins
        • ./debug
        • ./timeout
      • modules
        • ./register
    • steal-stache
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Ecosystem
  • /
  • can-zone
  • / On this page
    • can-zone

      module
      • npm package badge
      • Star
      • source

      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

      1. 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.

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.