Last active
March 28, 2017 00:12
-
-
Save tschaub/00cead930df134336fde43f0dc965f52 to your computer and use it in GitHub Desktop.
Test polyfill.io Promise in a Worker
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> | |
<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> |
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 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}); |
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://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