Skip to content

Instantly share code, notes, and snippets.

@redgeoff
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redgeoff/75df973e16895f6fb1ce to your computer and use it in GitHub Desktop.
Save redgeoff/75df973e16895f6fb1ce to your computer and use it in GitHub Desktop.
bluebird-chain-promises
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/1.2.2/bluebird.js"></script>
<script>
function setTimeoutPromise(ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
});
}
function foo(item, ms) {
return function() {
return setTimeoutPromise(ms).then(function () {
document.write(item + '<br/>');
});
};
}
var items = ['one', 'two', 'three'];
function bar() {
var chain = Promise.resolve();
items.forEach(function (el, i) {
chain = chain.then(foo(el, (items.length - i)*1000));
});
return chain;
}
bar().then(function () {
document.write('done<br/>');
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment