self-addressed
Version:
Transform postMessage to return a promise
38 lines (32 loc) • 1.04 kB
HTML
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>iframed page</title>
<script src="../../bower_components/es5-shim/es5-shim.js"></script>
</head>
<body>
<h2>this page is iframed</h2>
<script src="../../dist/self-addressed.js"></script>
<script src="../../node_modules/lazy-ass/index.js"></script>
<script>
console.log('loading page.html');
la(typeof selfAddressed === 'function', 'cannot find selfAddressed fn');
function mailman(address, msg) {
address.postMessage(msg, '*');
}
window.onmessage = function (event) {
console.log('page has received event', JSON.stringify(event.data));
selfAddressed(event.data);
};
selfAddressed(mailman, parent, 'foo')
.then(function (response) {
console.log('got response from parent iframe', response);
if (response !== 'bar') {
console.log('throwing error');
throw new Error('page has received wrong response ' + response);
}
});
</script>
</body>
</html>