IsPromise.js 480 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var callBound = require('call-bind/callBound');
  3. var $PromiseThen = callBound('Promise.prototype.then', true);
  4. var Type = require('./Type');
  5. // https://262.ecma-international.org/6.0/#sec-ispromise
  6. module.exports = function IsPromise(x) {
  7. if (Type(x) !== 'Object') {
  8. return false;
  9. }
  10. if (!$PromiseThen) { // Promises are not supported
  11. return false;
  12. }
  13. try {
  14. $PromiseThen(x); // throws if not a promise
  15. } catch (e) {
  16. return false;
  17. }
  18. return true;
  19. };