Skip to content

Instantly share code, notes, and snippets.

@monjer
Last active June 27, 2019 01:41
Show Gist options
  • Select an option

  • Save monjer/e5eb366d3bacaf73f2ae9553c5b682bb to your computer and use it in GitHub Desktop.

Select an option

Save monjer/e5eb366d3bacaf73f2ae9553c5b682bb to your computer and use it in GitHub Desktop.
const schedule = (() => {
const taskQueue = [];
const runTask = () => {
while (taskQueue.length > 0) {
const task = taskQueue.shift();
task();
}
};
const isArray = (obj) => {
return Object.prototype.toString.call(obj) === "[object Array]";
};
const node = document.createTextNode("");
const observer = new MutationObserver(() => {
runTask();
});
observer.observe(node, { characterData: true });
return {
push(tasks) {
tasks = isArray(tasks) ? tasks : [tasks];
taskQueue.push(...tasks);
node.textContent = node.textContent === "0" ? "1" : "0";
},
};
})();
@monjer

monjer commented Jun 26, 2019

Copy link
Copy Markdown
Author
function a() {
    console.log("a");
}
function b() {
    console.log("b");
}
setTimeout(() => {
    console.log("c");
}, 0);
var p = new Promise((resolve) => {
    console.log("d");
    resolve();
});
schedule.push([a, b]); 
// output : d a b c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment