asyncify.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = asyncify;
  6. var _isObject = require('lodash/isObject');
  7. var _isObject2 = _interopRequireDefault(_isObject);
  8. var _initialParams = require('./internal/initialParams');
  9. var _initialParams2 = _interopRequireDefault(_initialParams);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * Take a sync function and make it async, passing its return value to a
  13. * callback. This is useful for plugging sync functions into a waterfall,
  14. * series, or other async functions. Any arguments passed to the generated
  15. * function will be passed to the wrapped function (except for the final
  16. * callback argument). Errors thrown will be passed to the callback.
  17. *
  18. * If the function passed to `asyncify` returns a Promise, that promises's
  19. * resolved/rejected state will be used to call the callback, rather than simply
  20. * the synchronous return value.
  21. *
  22. * This also means you can asyncify ES2016 `async` functions.
  23. *
  24. * @name asyncify
  25. * @static
  26. * @memberOf module:Utils
  27. * @method
  28. * @alias wrapSync
  29. * @category Util
  30. * @param {Function} func - The synchronous function to convert to an
  31. * asynchronous function.
  32. * @returns {Function} An asynchronous wrapper of the `func`. To be invoked with
  33. * (callback).
  34. * @example
  35. *
  36. * // passing a regular synchronous function
  37. * async.waterfall([
  38. * async.apply(fs.readFile, filename, "utf8"),
  39. * async.asyncify(JSON.parse),
  40. * function (data, next) {
  41. * // data is the result of parsing the text.
  42. * // If there was a parsing error, it would have been caught.
  43. * }
  44. * ], callback);
  45. *
  46. * // passing a function returning a promise
  47. * async.waterfall([
  48. * async.apply(fs.readFile, filename, "utf8"),
  49. * async.asyncify(function (contents) {
  50. * return db.model.create(contents);
  51. * }),
  52. * function (model, next) {
  53. * // `model` is the instantiated model object.
  54. * // If there was an error, this function would be skipped.
  55. * }
  56. * ], callback);
  57. *
  58. * // es6 example
  59. * var q = async.queue(async.asyncify(async function(file) {
  60. * var intermediateStep = await processFile(file);
  61. * return await somePromise(intermediateStep)
  62. * }));
  63. *
  64. * q.push(files);
  65. */
  66. function asyncify(func) {
  67. return (0, _initialParams2.default)(function (args, callback) {
  68. var result;
  69. try {
  70. result = func.apply(this, args);
  71. } catch (e) {
  72. return callback(e);
  73. }
  74. // if result is Promise object
  75. if ((0, _isObject2.default)(result) && typeof result.then === 'function') {
  76. result.then(function (value) {
  77. callback(null, value);
  78. }, function (err) {
  79. callback(err.message ? err : new Error(err));
  80. });
  81. } else {
  82. callback(null, result);
  83. }
  84. });
  85. }
  86. module.exports = exports['default'];