Skip to content

Instantly share code, notes, and snippets.

@claudioacioli
Created November 13, 2024 14:27
Show Gist options
  • Save claudioacioli/b573cb0786f6aac7ca2fd4d50c095b73 to your computer and use it in GitHub Desktop.
Save claudioacioli/b573cb0786f6aac7ca2fd4d50c095b73 to your computer and use it in GitHub Desktop.
const schedule = (fileName) => {
const worker = new Worker(fileName);
return data => new Promise((resolve) => {
worker.addEventListener("message", function message (e) {
worker.removeEventListener("message", message);
return resolve(e.data);
});
worker.postMessage(data);
});
};
export default schedule;
@claudioacioli
Copy link
Author

A simple way to schedule actions and get promised reactions with js worker.

Usage:

import schedule from "schedule";

const action = schedule("filename.js");
const reaction = await action("data sended");
console.log(reaction);

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