Skip to content

Instantly share code, notes, and snippets.

@tschaub
Last active March 28, 2017 00:12
Show Gist options
  • Save tschaub/00cead930df134336fde43f0dc965f52 to your computer and use it in GitHub Desktop.
Save tschaub/00cead930df134336fde43f0dc965f52 to your computer and use it in GitHub Desktop.
Test polyfill.io Promise in a Worker
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test Promise.resolve() in a Worker</title>
</head>
<body>
This is a simple smoke test for <code>Promise.resolve()</code> in a Worker.
You should see an alert telling you if things worked or not.
<script src="./main.js"></script>
</body>
</html>
var worker = new Worker('./worker.js');
var id = 'Promise.resolve()';
worker.addEventListener('message', function(event) {
var data = event.data;
if (!data.success) {
alert(data.id + ' failed: ' + data.message);
} else {
alert(data.id + ' worked');
}
});
worker.postMessage({id: id});
importScripts('https://cdn.polyfill.io/v2/polyfill.min.js?features=Promise');
self.addEventListener('message', function(event) {
// we could have multiple listeners for different event.data
// instead we just do a simple smoke test for Promise.resolve()
var id = event.data.id;
try {
Promise.resolve()
.then(function() {
self.postMessage({id: id, success: true});
})
.catch(function() {
self.postMessage({id: id, success: false, message: 'resolve rejected!'});
});
} catch (error) {
self.postMessage({id: id, success: false, message: error.message});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment