Last active
November 4, 2015 13:44
-
-
Save derekstavis/d68e679112282f074c6b to your computer and use it in GitHub Desktop.
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 Q = require('q') | |
, R = require('ramda'); | |
function play(message) { | |
return Q.Promise(function (resolve, reject) { | |
setTimeout(function () { | |
console.log('Playing ' + message) | |
resolve(); | |
}, 3000); | |
}); | |
} | |
function playChain(messages) { | |
return R.reduce( | |
Q.when, | |
play(R.head(messages)), | |
R.map( | |
R.curry(R.partial)(play), | |
R.map(Array, R.tail(messages)) | |
) | |
); | |
} | |
playChain(['Message 1', 'Message 2', 'Message 3', 'Message 4']) | |
.then(function () { | |
console.log('All messages played!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment