draft: refactor project so it sits on top of a standalone app
This commit is contained in:
+41
@@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = until;
|
||||
|
||||
var _whilst = require('./whilst');
|
||||
|
||||
var _whilst2 = _interopRequireDefault(_whilst);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
/**
|
||||
* Repeatedly call `iteratee` until `test` returns `true`. Calls `callback` when
|
||||
* stopped, or an error occurs. `callback` will be passed an error and any
|
||||
* arguments passed to the final `iteratee`'s callback.
|
||||
*
|
||||
* The inverse of [whilst]{@link module:ControlFlow.whilst}.
|
||||
*
|
||||
* @name until
|
||||
* @static
|
||||
* @memberOf module:ControlFlow
|
||||
* @method
|
||||
* @see [async.whilst]{@link module:ControlFlow.whilst}
|
||||
* @category Control Flow
|
||||
* @param {Function} test - synchronous truth test to perform before each
|
||||
* execution of `iteratee`. Invoked with ().
|
||||
* @param {AsyncFunction} iteratee - An async function which is called each time
|
||||
* `test` fails. Invoked with (callback).
|
||||
* @param {Function} [callback] - A callback which is called after the test
|
||||
* function has passed and repeated execution of `iteratee` has stopped. `callback`
|
||||
* will be passed an error and any arguments passed to the final `iteratee`'s
|
||||
* callback. Invoked with (err, [results]);
|
||||
*/
|
||||
function until(test, iteratee, callback) {
|
||||
(0, _whilst2.default)(function () {
|
||||
return !test.apply(this, arguments);
|
||||
}, iteratee, callback);
|
||||
}
|
||||
module.exports = exports['default'];
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
exports.default = function (tasks, callback) {
|
||||
callback = (0, _once2.default)(callback || _noop2.default);
|
||||
if (!(0, _isArray2.default)(tasks)) return callback(new Error('First argument to waterfall must be an array of functions'));
|
||||
if (!tasks.length) return callback();
|
||||
var taskIndex = 0;
|
||||
|
||||
function nextTask(args) {
|
||||
var task = (0, _wrapAsync2.default)(tasks[taskIndex++]);
|
||||
args.push((0, _onlyOnce2.default)(next));
|
||||
task.apply(null, args);
|
||||
}
|
||||
|
||||
function next(err /*, ...args*/) {
|
||||
if (err || taskIndex === tasks.length) {
|
||||
return callback.apply(null, arguments);
|
||||
}
|
||||
nextTask((0, _slice2.default)(arguments, 1));
|
||||
}
|
||||
|
||||
nextTask([]);
|
||||
};
|
||||
|
||||
var _isArray = require('lodash/isArray');
|
||||
|
||||
var _isArray2 = _interopRequireDefault(_isArray);
|
||||
|
||||
var _noop = require('lodash/noop');
|
||||
|
||||
var _noop2 = _interopRequireDefault(_noop);
|
||||
|
||||
var _once = require('./internal/once');
|
||||
|
||||
var _once2 = _interopRequireDefault(_once);
|
||||
|
||||
var _slice = require('./internal/slice');
|
||||
|
||||
var _slice2 = _interopRequireDefault(_slice);
|
||||
|
||||
var _onlyOnce = require('./internal/onlyOnce');
|
||||
|
||||
var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
|
||||
|
||||
var _wrapAsync = require('./internal/wrapAsync');
|
||||
|
||||
var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
module.exports = exports['default'];
|
||||
|
||||
/**
|
||||
* Runs the `tasks` array of functions in series, each passing their results to
|
||||
* the next in the array. However, if any of the `tasks` pass an error to their
|
||||
* own callback, the next function is not executed, and the main `callback` is
|
||||
* immediately called with the error.
|
||||
*
|
||||
* @name waterfall
|
||||
* @static
|
||||
* @memberOf module:ControlFlow
|
||||
* @method
|
||||
* @category Control Flow
|
||||
* @param {Array} tasks - An array of [async functions]{@link AsyncFunction}
|
||||
* to run.
|
||||
* Each function should complete with any number of `result` values.
|
||||
* The `result` values will be passed as arguments, in order, to the next task.
|
||||
* @param {Function} [callback] - An optional callback to run once all the
|
||||
* functions have completed. This will be passed the results of the last task's
|
||||
* callback. Invoked with (err, [results]).
|
||||
* @returns undefined
|
||||
* @example
|
||||
*
|
||||
* async.waterfall([
|
||||
* function(callback) {
|
||||
* callback(null, 'one', 'two');
|
||||
* },
|
||||
* function(arg1, arg2, callback) {
|
||||
* // arg1 now equals 'one' and arg2 now equals 'two'
|
||||
* callback(null, 'three');
|
||||
* },
|
||||
* function(arg1, callback) {
|
||||
* // arg1 now equals 'three'
|
||||
* callback(null, 'done');
|
||||
* }
|
||||
* ], function (err, result) {
|
||||
* // result now equals 'done'
|
||||
* });
|
||||
*
|
||||
* // Or, with named functions:
|
||||
* async.waterfall([
|
||||
* myFirstFunction,
|
||||
* mySecondFunction,
|
||||
* myLastFunction,
|
||||
* ], function (err, result) {
|
||||
* // result now equals 'done'
|
||||
* });
|
||||
* function myFirstFunction(callback) {
|
||||
* callback(null, 'one', 'two');
|
||||
* }
|
||||
* function mySecondFunction(arg1, arg2, callback) {
|
||||
* // arg1 now equals 'one' and arg2 now equals 'two'
|
||||
* callback(null, 'three');
|
||||
* }
|
||||
* function myLastFunction(arg1, callback) {
|
||||
* // arg1 now equals 'three'
|
||||
* callback(null, 'done');
|
||||
* }
|
||||
*/
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = whilst;
|
||||
|
||||
var _noop = require('lodash/noop');
|
||||
|
||||
var _noop2 = _interopRequireDefault(_noop);
|
||||
|
||||
var _slice = require('./internal/slice');
|
||||
|
||||
var _slice2 = _interopRequireDefault(_slice);
|
||||
|
||||
var _onlyOnce = require('./internal/onlyOnce');
|
||||
|
||||
var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
|
||||
|
||||
var _wrapAsync = require('./internal/wrapAsync');
|
||||
|
||||
var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
/**
|
||||
* Repeatedly call `iteratee`, while `test` returns `true`. Calls `callback` when
|
||||
* stopped, or an error occurs.
|
||||
*
|
||||
* @name whilst
|
||||
* @static
|
||||
* @memberOf module:ControlFlow
|
||||
* @method
|
||||
* @category Control Flow
|
||||
* @param {Function} test - synchronous truth test to perform before each
|
||||
* execution of `iteratee`. Invoked with ().
|
||||
* @param {AsyncFunction} iteratee - An async function which is called each time
|
||||
* `test` passes. Invoked with (callback).
|
||||
* @param {Function} [callback] - A callback which is called after the test
|
||||
* function has failed and repeated execution of `iteratee` has stopped. `callback`
|
||||
* will be passed an error and any arguments passed to the final `iteratee`'s
|
||||
* callback. Invoked with (err, [results]);
|
||||
* @returns undefined
|
||||
* @example
|
||||
*
|
||||
* var count = 0;
|
||||
* async.whilst(
|
||||
* function() { return count < 5; },
|
||||
* function(callback) {
|
||||
* count++;
|
||||
* setTimeout(function() {
|
||||
* callback(null, count);
|
||||
* }, 1000);
|
||||
* },
|
||||
* function (err, n) {
|
||||
* // 5 seconds have passed, n = 5
|
||||
* }
|
||||
* );
|
||||
*/
|
||||
function whilst(test, iteratee, callback) {
|
||||
callback = (0, _onlyOnce2.default)(callback || _noop2.default);
|
||||
var _iteratee = (0, _wrapAsync2.default)(iteratee);
|
||||
if (!test()) return callback(null);
|
||||
var next = function (err /*, ...args*/) {
|
||||
if (err) return callback(err);
|
||||
if (test()) return _iteratee(next);
|
||||
var args = (0, _slice2.default)(arguments, 1);
|
||||
callback.apply(null, [null].concat(args));
|
||||
};
|
||||
_iteratee(next);
|
||||
}
|
||||
module.exports = exports['default'];
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = asyncify;
|
||||
|
||||
var _isObject = require('lodash/isObject');
|
||||
|
||||
var _isObject2 = _interopRequireDefault(_isObject);
|
||||
|
||||
var _initialParams = require('./internal/initialParams');
|
||||
|
||||
var _initialParams2 = _interopRequireDefault(_initialParams);
|
||||
|
||||
var _setImmediate = require('./internal/setImmediate');
|
||||
|
||||
var _setImmediate2 = _interopRequireDefault(_setImmediate);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
/**
|
||||
* Take a sync function and make it async, passing its return value to a
|
||||
* callback. This is useful for plugging sync functions into a waterfall,
|
||||
* series, or other async functions. Any arguments passed to the generated
|
||||
* function will be passed to the wrapped function (except for the final
|
||||
* callback argument). Errors thrown will be passed to the callback.
|
||||
*
|
||||
* If the function passed to `asyncify` returns a Promise, that promises's
|
||||
* resolved/rejected state will be used to call the callback, rather than simply
|
||||
* the synchronous return value.
|
||||
*
|
||||
* This also means you can asyncify ES2017 `async` functions.
|
||||
*
|
||||
* @name asyncify
|
||||
* @static
|
||||
* @memberOf module:Utils
|
||||
* @method
|
||||
* @alias wrapSync
|
||||
* @category Util
|
||||
* @param {Function} func - The synchronous function, or Promise-returning
|
||||
* function to convert to an {@link AsyncFunction}.
|
||||
* @returns {AsyncFunction} An asynchronous wrapper of the `func`. To be
|
||||
* invoked with `(args..., callback)`.
|
||||
* @example
|
||||
*
|
||||
* // passing a regular synchronous function
|
||||
* async.waterfall([
|
||||
* async.apply(fs.readFile, filename, "utf8"),
|
||||
* async.asyncify(JSON.parse),
|
||||
* function (data, next) {
|
||||
* // data is the result of parsing the text.
|
||||
* // If there was a parsing error, it would have been caught.
|
||||
* }
|
||||
* ], callback);
|
||||
*
|
||||
* // passing a function returning a promise
|
||||
* async.waterfall([
|
||||
* async.apply(fs.readFile, filename, "utf8"),
|
||||
* async.asyncify(function (contents) {
|
||||
* return db.model.create(contents);
|
||||
* }),
|
||||
* function (model, next) {
|
||||
* // `model` is the instantiated model object.
|
||||
* // If there was an error, this function would be skipped.
|
||||
* }
|
||||
* ], callback);
|
||||
*
|
||||
* // es2017 example, though `asyncify` is not needed if your JS environment
|
||||
* // supports async functions out of the box
|
||||
* var q = async.queue(async.asyncify(async function(file) {
|
||||
* var intermediateStep = await processFile(file);
|
||||
* return await somePromise(intermediateStep)
|
||||
* }));
|
||||
*
|
||||
* q.push(files);
|
||||
*/
|
||||
function asyncify(func) {
|
||||
return (0, _initialParams2.default)(function (args, callback) {
|
||||
var result;
|
||||
try {
|
||||
result = func.apply(this, args);
|
||||
} catch (e) {
|
||||
return callback(e);
|
||||
}
|
||||
// if result is Promise object
|
||||
if ((0, _isObject2.default)(result) && typeof result.then === 'function') {
|
||||
result.then(function (value) {
|
||||
invokeCallback(callback, null, value);
|
||||
}, function (err) {
|
||||
invokeCallback(callback, err.message ? err : new Error(err));
|
||||
});
|
||||
} else {
|
||||
callback(null, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function invokeCallback(callback, error, value) {
|
||||
try {
|
||||
callback(error, value);
|
||||
} catch (e) {
|
||||
(0, _setImmediate2.default)(rethrow, e);
|
||||
}
|
||||
}
|
||||
|
||||
function rethrow(error) {
|
||||
throw error;
|
||||
}
|
||||
module.exports = exports['default'];
|
||||
Reference in New Issue
Block a user