Created
October 31, 2014 09:10
-
-
Save richardj/ce020fe077f95c690c91 to your computer and use it in GitHub Desktop.
// source http://jsbin.com/xicego
This file contains hidden or 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 name="viewport" content="width=device-width"> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
<style id="jsbin-css"> | |
html { | |
font-family: sans-serif; | |
} | |
</style> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function wait(ms) { | |
return new Promise(function(r) { | |
setTimeout(r, ms); | |
}); | |
} | |
function randomWait() { | |
return wait(Math.random() * 5000); | |
} | |
function fetchHello() { | |
return randomWait().then(function() { | |
return "Hello"; | |
}); | |
} | |
function fetchWorld() { | |
return randomWait().then(function() { | |
return "World"; | |
}); | |
} | |
function fetchFoobar() { | |
return randomWait().then(function() { | |
return "Foobar"; | |
}); | |
} | |
[fetchHello(), fetchWorld(), fetchFoobar()].reduce(function(chain, promise) { | |
return chain.then(function() { | |
return promise; | |
}).then(function(value) { | |
document.body.appendChild( | |
document.createTextNode(value + ' ') | |
); | |
}); | |
}, Promise.resolve()); | |
</script> | |
<script id="jsbin-source-css" type="text/css">html { | |
font-family: sans-serif; | |
}</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function wait(ms) { | |
return new Promise(function(r) { | |
setTimeout(r, ms); | |
}); | |
} | |
function randomWait() { | |
return wait(Math.random() * 5000); | |
} | |
function fetchHello() { | |
return randomWait().then(function() { | |
return "Hello"; | |
}); | |
} | |
function fetchWorld() { | |
return randomWait().then(function() { | |
return "World"; | |
}); | |
} | |
function fetchFoobar() { | |
return randomWait().then(function() { | |
return "Foobar"; | |
}); | |
} | |
[fetchHello(), fetchWorld(), fetchFoobar()].reduce(function(chain, promise) { | |
return chain.then(function() { | |
return promise; | |
}).then(function(value) { | |
document.body.appendChild( | |
document.createTextNode(value + ' ') | |
); | |
}); | |
}, Promise.resolve());</script></body> | |
</html> |
This file contains hidden or 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
html { | |
font-family: sans-serif; | |
} |
This file contains hidden or 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
function wait(ms) { | |
return new Promise(function(r) { | |
setTimeout(r, ms); | |
}); | |
} | |
function randomWait() { | |
return wait(Math.random() * 5000); | |
} | |
function fetchHello() { | |
return randomWait().then(function() { | |
return "Hello"; | |
}); | |
} | |
function fetchWorld() { | |
return randomWait().then(function() { | |
return "World"; | |
}); | |
} | |
function fetchFoobar() { | |
return randomWait().then(function() { | |
return "Foobar"; | |
}); | |
} | |
[fetchHello(), fetchWorld(), fetchFoobar()].reduce(function(chain, promise) { | |
return chain.then(function() { | |
return promise; | |
}).then(function(value) { | |
document.body.appendChild( | |
document.createTextNode(value + ' ') | |
); | |
}); | |
}, Promise.resolve()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment