Last active
March 1, 2024 22:47
-
-
Save nolanlawson/05e74a8408a099635c9a38f839b5ae9f to your computer and use it in GitHub Desktop.
PromiseWorker WebWorker example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<title> | |
PromiseWorker WebWorker example | |
</title> | |
<body> | |
<h1>PromiseWorker WebWorker example</h1> | |
<pre id="display"></pre> | |
<script src="https://npmcdn.com/promise-worker/dist/promise-worker.js"></script> | |
<script src="main.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var worker = new Worker('worker.js'); | |
var promiseWorker = new PromiseWorker(worker); | |
function log(str) { | |
console.log(str); | |
display.innerHTML += str + '\n'; | |
} | |
var msg = {hello: 'worker'}; | |
log('Sending message to worker: ' + JSON.stringify(msg)); | |
promiseWorker.postMessage(msg).then(function (response) { | |
log('Got response: ' + JSON.stringify(response)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
importScripts('https://npmcdn.com/promise-worker/dist/promise-worker.register.js'); | |
registerPromiseWorker(function (msg) { | |
return {hello: 'main thread'}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment