shim.js 706 B

123456789101112131415161718192021222324
  1. "use strict";
  2. var ensurePlainFunction = require("../../../object/ensure-plain-function")
  3. , isThenable = require("../../../object/is-thenable")
  4. , ensureThenable = require("../../../object/ensure-thenable");
  5. var resolveCallback = function (callback, next) {
  6. var callbackResult = callback();
  7. if (!isThenable(callbackResult)) return next();
  8. return callbackResult.then(next);
  9. };
  10. module.exports = function (callback) {
  11. ensureThenable(this);
  12. ensurePlainFunction(callback);
  13. return this.then(
  14. function (result) {
  15. return resolveCallback(callback, function () { return result; });
  16. },
  17. function (error) {
  18. return resolveCallback(callback, function () { throw error; });
  19. }
  20. );
  21. };