react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / har-validator / node_modules / bluebird / js / main / thenables.js
81160 views"use strict";1module.exports = function(Promise, INTERNAL) {2var util = require("./util.js");3var errorObj = util.errorObj;4var isObject = util.isObject;56function tryConvertToPromise(obj, context) {7if (isObject(obj)) {8if (obj instanceof Promise) {9return obj;10}11else if (isAnyBluebirdPromise(obj)) {12var ret = new Promise(INTERNAL);13obj._then(14ret._fulfillUnchecked,15ret._rejectUncheckedCheckError,16ret._progressUnchecked,17ret,18null19);20return ret;21}22var then = util.tryCatch(getThen)(obj);23if (then === errorObj) {24if (context) context._pushContext();25var ret = Promise.reject(then.e);26if (context) context._popContext();27return ret;28} else if (typeof then === "function") {29return doThenable(obj, then, context);30}31}32return obj;33}3435function getThen(obj) {36return obj.then;37}3839var hasProp = {}.hasOwnProperty;40function isAnyBluebirdPromise(obj) {41return hasProp.call(obj, "_promise0");42}4344function doThenable(x, then, context) {45var promise = new Promise(INTERNAL);46var ret = promise;47if (context) context._pushContext();48promise._captureStackTrace();49if (context) context._popContext();50var synchronous = true;51var result = util.tryCatch(then).call(x,52resolveFromThenable,53rejectFromThenable,54progressFromThenable);55synchronous = false;56if (promise && result === errorObj) {57promise._rejectCallback(result.e, true, true);58promise = null;59}6061function resolveFromThenable(value) {62if (!promise) return;63if (x === value) {64promise._rejectCallback(65Promise._makeSelfResolutionError(), false, true);66} else {67promise._resolveCallback(value);68}69promise = null;70}7172function rejectFromThenable(reason) {73if (!promise) return;74promise._rejectCallback(reason, synchronous, true);75promise = null;76}7778function progressFromThenable(value) {79if (!promise) return;80if (typeof promise._progress === "function") {81promise._progress(value);82}83}84return ret;85}8687return tryConvertToPromise;88};899091