defer/defer.js
function postMessage(ctx) {
    var queue = [],
      message = "nextTick";

    function handle(event) {

      if (event.source === ctx && event.data === message) {

        if (!!event.stopPropogation) {

          event.stopPropogation();

        }

        if (queue.length > 0) {

          queue.shift()();

        }

      }

    }

    if (!!ctx.addEventListener) {

      ctx.addEventListener("message", handle, true);

    } else {

      ctx.attachEvent("onmessage", handle);

    }

    return function defer(fn) {

      queue.push(fn);
      ctx.postMessage(message, '*');

    };
  }
function postMessage(ctx) {
    var queue = [],
      message = "nextTick";

    function handle(event) {

      if (event.source === ctx && event.data === message) {

        if (!!event.stopPropogation) {

          event.stopPropogation();

        }

        if (queue.length > 0) {

          queue.shift()();

        }

      }

    }

    if (!!ctx.addEventListener) {

      ctx.addEventListener("message", handle, true);

    } else {

      ctx.attachEvent("onmessage", handle);

    }

    return function defer(fn) {

      queue.push(fn);
      ctx.postMessage(message, '*');

    };
  }
function postMessage ( ctx ) { var queue = [ ] , message = "nextTick" ; function handle ( event ) { if ( event . source === ctx && event . data === message ) { if ( ! ! event . stopPropogation ) { event . stopPropogation ( ) ; } if ( queue . length > 0 ) { queue . shift ( ) ( ) ; } } } if ( ! ! ctx . addEventListener ) { ctx . addEventListener ( "message" , handle , true ) ; } else { ctx . attachEvent ( "onmessage" , handle ) ; } return function defer ( fn ) { queue . push ( fn ) ; ctx . postMessage ( message , '*' ) ; } ; }
window.postMessage is refered to quite a bit in articles discussing a potential setZeroTimeout for browsers. The problem it attempts to solve is that setTimeout has a minimum wait time in all browsers. This means your function is not scheduled to run on the next cycle of the event loop but, rather, at the next cycle of the event loop after the timeout has passed.  Instead, this method uses a message passing features that has been integrated into modern browsers to replicate the functionality of process.nextTick.
window . postMessage is refered to quite a bit in articles discussing a potential setZeroTimeout for browsers . The problem it attempts to solve is that setTimeout has a minimum wait time in all browsers . This means your function is not scheduled to run on the next cycle of the event loop but rather at the next cycle of the event loop after the timeout has passed . Instead this method uses a message passing features that has been integrated into modern browsers to replicate the functionality of process . nextTick .