Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

seq.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = seq;
  6. var _noop = require('lodash/noop');
  7. var _noop2 = _interopRequireDefault(_noop);
  8. var _slice = require('./internal/slice');
  9. var _slice2 = _interopRequireDefault(_slice);
  10. var _reduce = require('./reduce');
  11. var _reduce2 = _interopRequireDefault(_reduce);
  12. var _wrapAsync = require('./internal/wrapAsync');
  13. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  14. var _arrayMap = require('lodash/_arrayMap');
  15. var _arrayMap2 = _interopRequireDefault(_arrayMap);
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. /**
  18. * Version of the compose function that is more natural to read. Each function
  19. * consumes the return value of the previous function. It is the equivalent of
  20. * [compose]{@link module:ControlFlow.compose} with the arguments reversed.
  21. *
  22. * Each function is executed with the `this` binding of the composed function.
  23. *
  24. * @name seq
  25. * @static
  26. * @memberOf module:ControlFlow
  27. * @method
  28. * @see [async.compose]{@link module:ControlFlow.compose}
  29. * @category Control Flow
  30. * @param {...AsyncFunction} functions - the asynchronous functions to compose
  31. * @returns {Function} a function that composes the `functions` in order
  32. * @example
  33. *
  34. * // Requires lodash (or underscore), express3 and dresende's orm2.
  35. * // Part of an app, that fetches cats of the logged user.
  36. * // This example uses `seq` function to avoid overnesting and error
  37. * // handling clutter.
  38. * app.get('/cats', function(request, response) {
  39. * var User = request.models.User;
  40. * async.seq(
  41. * _.bind(User.get, User), // 'User.get' has signature (id, callback(err, data))
  42. * function(user, fn) {
  43. * user.getCats(fn); // 'getCats' has signature (callback(err, data))
  44. * }
  45. * )(req.session.user_id, function (err, cats) {
  46. * if (err) {
  47. * console.error(err);
  48. * response.json({ status: 'error', message: err.message });
  49. * } else {
  50. * response.json({ status: 'ok', message: 'Cats found', data: cats });
  51. * }
  52. * });
  53. * });
  54. */
  55. function seq() /*...functions*/{
  56. var _functions = (0, _arrayMap2.default)(arguments, _wrapAsync2.default);
  57. return function () /*...args*/{
  58. var args = (0, _slice2.default)(arguments);
  59. var that = this;
  60. var cb = args[args.length - 1];
  61. if (typeof cb == 'function') {
  62. args.pop();
  63. } else {
  64. cb = _noop2.default;
  65. }
  66. (0, _reduce2.default)(_functions, args, function (newargs, fn, cb) {
  67. fn.apply(that, newargs.concat(function (err /*, ...nextargs*/) {
  68. var nextargs = (0, _slice2.default)(arguments, 1);
  69. cb(err, nextargs);
  70. }));
  71. }, function (err, results) {
  72. cb.apply(that, [err].concat(results));
  73. });
  74. };
  75. }
  76. module.exports = exports['default'];