Skip to content

Instantly share code, notes, and snippets.

@victorporof
Last active May 30, 2022 06:25
Show Gist options
  • Save victorporof/360caf41f7442c1e4829484af082affb to your computer and use it in GitHub Desktop.
Save victorporof/360caf41f7442c1e4829484af082affb to your computer and use it in GitHub Desktop.
Async Stack Tagging API (Non-Recurring)
/* --- Userland --- */
function someTask() {
console.trace("completeWork: someTask");
}
function someOtherTask() {
console.trace("completeWork: someOtherTask");
}
function businessLogic() {
scheduler.scheduleUnitOfWork(someTask);
scheduler.scheduleUnitOfWork(someOtherTask);
}
businessLogic();
scheduler.workLoop();
/* --- Framework --- */
function makeScheduler() {
let tasks = [];
return {
scheduleUnitOfWork(f) {
const id = console.scheduleAsyncTask(f.name);
tasks.push({ id, f });
},
workLoop() {
while (tasks.length) {
const { id, f } = tasks.shift();
console.startAsyncTask(id);
f();
console.finishAsyncTask(id);
}
},
};
}
const scheduler = makeScheduler();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Async Tasks API</title>
</head>
<body>
<script src="framework.js"></script>
<script src="async-stack-tagging-non-recurring.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment