Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A setImmediate polyfill optimized for size.
(function(global, name, mc, counter, slice){
if (!(name in global)) {
if (mc = mc in global && new global[mc]) {
mc.port1.onmessage = function(evt) {
slice[evt = evt.data]();
delete slice[evt];
};
global[name] = function(cb) {
var args = slice.call(arguments, 1);
slice[++counter] = function() { cb.apply(0, args); };
mc.port2.postMessage(counter);
}
}
}
})(this, 'setImmediate', 'MessageChannel', 0, [].slice);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment