Last active
August 29, 2015 14:05
-
-
Save redgeoff/75df973e16895f6fb1ce to your computer and use it in GitHub Desktop.
bluebird-chain-promises
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
<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