DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
  • Ecosystem
  • Infrastructure
    • can-construct
    • can-control
    • can-event
    • can-event/async/async
      • async
      • dispatch
      • flush
      • queue
      • sync
    • can-event/batch/batch
    • can-observation
    • can-simple-map
    • can-util
    • can-view-callbacks
    • can-view-live
    • can-view-model
    • can-view-nodelist
    • can-view-parser
    • can-view-scope
    • can-view-target
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Infrastructure
  • /
  • can-event/async/async
  • / On this page
    • can-event/async/async

      module

      Makes the event system asynchronous. WARNING: This is experimental technology.

      • source

      Object

      The can-event/async/async module makes the event system asynchronous. It:

      • Provides an async method which converts event binding and dispatching to happen asynchronously.
      • Provides sync method which converts event binding and dispatching to happen synchronously.
      • Provides an asynchronous dispatch, queue, [can-event/async/async.addEventListener] and [can-event/async/async.removeEventListener].
      • Provides a flush which can be used to immediately run all tasks in the task queue.

      Use

      Use can-event/async/async's async method to make event binding and dispatching happen immediately following the current event loop.

      var canEvent = require("can-event");
      var canAsync = require("can-event/async/async");
      canAsync.async();
      
      var obj = {};
      Object.assign(obj, canEvent);
      
      obj.addEventListener("foo", function(){
        console.log("heard foo");
      });
      obj.dispatch("foo");
      console.log("dispatched foo");
      
      // Logs -> "dispatched foo" then "heard foo"
      

      This means you never have to call start and stop. Notice that in the following example "change" is only fired once:

      var canAsync = require("can-event/async/async");
      canAsync.async();
      
      var compute = require("can-compute");
      
      var first = compute("Justin");
      var last = compute("Meyer");
      
      var fullName = compute(function(){
          return first() + " " + last();
      });
      
      fullName.on("change", function(ev, newVal, oldVal){
          newVal //-> "Payal Shah"
          oldVal //-> "Justin Meyer"
      });
      
      first("Payal");
      last("Shah");
      

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