Created
May 8, 2017 10:20
-
-
Save Kamisama666/400a1eda96e945f3262d0a1b190bc2c1 to your computer and use it in GitHub Desktop.
Maps a function that returns a promise and then passes that array of promises to Q.all so they are all processed. Uses Fn.js and Q. Example included
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
"use strict"; | |
const Q = require('q'); | |
const fn = require('fn.js'); | |
// Maps a function that returns a promise and then passes that array of promises to Q.all so they are all processed | |
const mapAll = fn.curry(fn.compose(Q.all, fn.map), 2); | |
// Returns a promise | |
function returnPromise(something) { | |
return Q().then(() => something); | |
} | |
const somethings = [1, 2, 3]; | |
// Example of use | |
Q() | |
.then(function() { | |
return somethings; | |
}) | |
.then(mapAll(returnPromise)) | |
.then(function(result) { | |
console.log(result) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment