|
@@ -2,7 +2,12 @@
|
|
|
|
|
|
const toArray = require('./array').toArray;
|
|
const toArray = require('./array').toArray;
|
|
|
|
|
|
-const argsToArray = fn => () => fn(toArray(arguments));
|
|
|
|
|
|
+const argsToArray = fn => {
|
|
|
|
+ // must not be a fat arrow since it breaks the arguments
|
|
|
|
+ return function () {
|
|
|
|
+ return fn(toArray(arguments));
|
|
|
|
+ };
|
|
|
|
+};
|
|
|
|
|
|
const curry = fn => {
|
|
const curry = fn => {
|
|
return function curried () {
|
|
return function curried () {
|
|
@@ -19,8 +24,70 @@ const curry = fn => {
|
|
|
|
|
|
const combine = argsToArray(fns => fns.reverse().reduce((fnc, fn) => val => fnc(fn(val))));
|
|
const combine = argsToArray(fns => fns.reverse().reduce((fnc, fn) => val => fnc(fn(val))));
|
|
|
|
|
|
|
|
+const promisify = fn => () => {
|
|
|
|
+ // copy passed arguments to a new array
|
|
|
|
+ const args = toArray(arguments);
|
|
|
|
+
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ // add a callback to the arguments, which resolves the promise with the result
|
|
|
|
+ args.push((err, data) => {
|
|
|
|
+ if (err) {
|
|
|
|
+ reject(err);
|
|
|
|
+ } else {
|
|
|
|
+ resolve(data);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ // call the original function with our callback flavoured arguments
|
|
|
|
+ fn.apply(null, args);
|
|
|
|
+ });
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ returns a function which executes promises one after another
|
|
|
|
+ @param promiseGenerators an array of functions which return a promise
|
|
|
|
+ @return function which executes the promises
|
|
|
|
+ */
|
|
|
|
+const enqueue = (promiseGenerators) => {
|
|
|
|
+ return promiseGenerators.reduce((f, promiseGenerator) => {
|
|
|
|
+ return () => {
|
|
|
|
+ return f().then(promiseGenerator);
|
|
|
|
+ };
|
|
|
|
+ });
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ Returns a function which executes promises one after another. The resulting function
|
|
|
|
+ returns a promise, which gets filled with an array of the results of the single promises.
|
|
|
|
+ @param promiseGenerators an array of functions which return a promise
|
|
|
|
+ @return function which executes the promises
|
|
|
|
+ */
|
|
|
|
+const enqueueWithResults = (function () {
|
|
|
|
+ const fnQueue = (results, promiseGenerators) => promiseGenerators.reduce((f, promiseGenerator) => {
|
|
|
|
+ return () => {
|
|
|
|
+ return f().then(result => {
|
|
|
|
+ results.push(result);
|
|
|
|
+ return promiseGenerator();
|
|
|
|
+ });
|
|
|
|
+ };
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return function (promiseGenerators) {
|
|
|
|
+ return () => {
|
|
|
|
+ const results = [];
|
|
|
|
+ return fnQueue(results, promiseGenerators)().then(result => {
|
|
|
|
+ results.push(result);
|
|
|
|
+ return results;
|
|
|
|
+ });
|
|
|
|
+ };
|
|
|
|
+ };
|
|
|
|
+}());
|
|
|
|
+
|
|
module.exports = {
|
|
module.exports = {
|
|
argsToArray,
|
|
argsToArray,
|
|
combine,
|
|
combine,
|
|
- curry
|
|
|
|
|
|
+ curry,
|
|
|
|
+ promisify,
|
|
|
|
+ enqueue,
|
|
|
|
+ enqueueWithResults
|
|
};
|
|
};
|