Skip to content

Instantly share code, notes, and snippets.

@ryancdotorg
Created January 3, 2021 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryancdotorg/731d9ff6422db126c5ea5631eb548df5 to your computer and use it in GitHub Desktop.
Save ryancdotorg/731d9ff6422db126c5ea5631eb548df5 to your computer and use it in GitHub Desktop.
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