DoneJS StealJS jQuery++ FuncUnit DocumentJS
4.3.0
5.0.0 3.13.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-define
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Data Modeling
      • can-connect
      • can-connect-feathers
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
        • types
          • ZoneSpec
          • makeZoneSpec
        • static
          • current
          • error
          • ignore
          • waitFor
        • prototype
          • addWait
          • data
          • removeWait
          • run
        • plugins
          • ./debug
          • ./timeout
        • modules
          • ./register
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-zone

  • npm package badge
  • Star
  • Edit on GitHub

A library that tracks asynchronous activity and lets you know when it has completed. Useful when you need to call a function and wait for all async behavior to complete, such as when performing server-side rendering.

new Zone()

Creates a new Zone with no additional overrides. Can then call zone.run to call a function within the Zone.

import Zone from "can-zone";

const zone = new Zone();

zone.run( function() {

    return "hello world";

} ).then( function( data ) {
    data.result; // -> "hello world"
} );

*Note: See the <a href="can-zone/register.html" title="In order to do it's magic, can-zone has to register handlers for all of the common JavaScript async operations. If you have code (or a dependency with this code) that does: const st = setTimeout;

And this module loads before can-zone, any time st is used we won't be able to track that within the Zone. To work around this, can-zone/register is used as a script that you run before any other modules. In Node require( "can-zone/register" );

At the top of your entry-point script. In the Browser You can either add a script tag above all others: <script src="node_modules/can-zone/register.js"></script>

Or, if you're using a module loader / bundler, configure it so that can-zone/register is placed above all others in the bundle.">docs(https://github.com/canjs/can-zone/blob/master/docs/register.md) about ensuring can-zone is registered properly.*

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.

import Zone from "can-zone";

const timeoutSpec = function() {
    let timeoutId;

    return {
        created: function() {
            timeoutId = setTimeout( function() {
                Zone.error( new Error( "This took too long!" ) );
            }, 5000 );
        },
        ended: function() {
            clearTimeout( timeoutId );
        }
    };
};

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

Tasks

JavaScript uses various task queues (and a microtask queue) to run JavaScript in the event loop. See this article and this StackOverflow answer to learn more.

For can-zone to work we have to override various task-creating functionality, this is the list of what we currently implement:

Macrotasks

  • setTimeout
  • XMLHttpRequest

Microtasks

  • requestAnimationFrame
  • Promise
  • process.nextTick

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:

const zone = new Zone();

This gives you a Zone from which you can run code using zone.run:

import Zone from "can-zone";

new Zone().run( function() {

    setTimeout( function() {

    }, 29 );

    setTimeout( function() {

    }, 13 );

    const xhr = new XMLHttpRequest();
    xhr.open( "GET", "http://chat.donejs.com/api/messages" );
    xhr.onload = function() {

    };
    xhr.send();

} ).then( function() {

    // All done!
} );

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.

Tasks

JavaScript uses various task queues (and a microtask queue) to run JavaScript in the event loop. See this article and this StackOverflow answer to learn more.

For can-zone to work we have to override various task-creating functionality, this is the list of what we currently implement:

Macrotasks

  • setTimeout
  • XMLHttpRequest

Microtasks

  • requestAnimationFrame
  • Promise
  • process.nextTick

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

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news