index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const utils_1 = require("./utils");
  4. function throwLater(e) {
  5. setTimeout(function () {
  6. throw e;
  7. }, 0);
  8. }
  9. function asCallback(promise, nodeback, options) {
  10. if (typeof nodeback === 'function') {
  11. promise.then((val) => {
  12. let ret;
  13. if (options !== undefined && Object(options).spread && Array.isArray(val)) {
  14. ret = utils_1.tryCatch(nodeback).apply(undefined, [null].concat(val));
  15. }
  16. else {
  17. ret = val === undefined
  18. ? utils_1.tryCatch(nodeback)(null)
  19. : utils_1.tryCatch(nodeback)(null, val);
  20. }
  21. if (ret === utils_1.errorObj) {
  22. throwLater(ret.e);
  23. }
  24. }, (cause) => {
  25. if (!cause) {
  26. const newReason = new Error(cause + '');
  27. Object.assign(newReason, { cause });
  28. cause = newReason;
  29. }
  30. const ret = utils_1.tryCatch(nodeback)(cause);
  31. if (ret === utils_1.errorObj) {
  32. throwLater(ret.e);
  33. }
  34. });
  35. }
  36. return promise;
  37. }
  38. exports.default = asCallback;