Skip to content

Instantly share code, notes, and snippets.

@rhoot
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save rhoot/e075b19acf8e6d6b2259 to your computer and use it in GitHub Desktop.

Select an option

Save rhoot/e075b19acf8e6d6b2259 to your computer and use it in GitHub Desktop.

Revisions

  1. rhoot revised this gist Apr 30, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions setImmediate.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    // Note: You probably do not want to use this in production code, as Promise is
    // not supported by all browsers yet.

    (function() {
    "use strict";

  2. rhoot revised this gist Apr 30, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions setImmediate.js
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,8 @@
    new Promise(function(resolve) {
    resolve(handle);
    }).then(onResolve);

    return handle;
    };

    window.clearImmediate = function(handle) {
  3. rhoot created this gist Apr 30, 2014.
    38 changes: 38 additions & 0 deletions setImmediate.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    (function() {
    "use strict";

    if (window.setImmediate) {
    return;
    }

    var pending = {},
    nextHandle = 1;

    function onResolve(handle) {
    var callback = pending[handle];
    if (callback) {
    delete pending[handle];
    callback.fn.apply(null, callback.args);
    }
    }

    window.setImmediate = function(fn) {
    var args = Array.prototype.slice.call(arguments, 1),
    handle;

    if (typeof fn !== "function") {
    throw new TypeError("invalid function");
    }

    handle = nextHandle++;
    pending[handle] = { fn: fn, args: args };

    new Promise(function(resolve) {
    resolve(handle);
    }).then(onResolve);
    };

    window.clearImmediate = function(handle) {
    delete pending[handle];
    };
    }());