123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- "use strict";
- module.exports =
- function(Promise, PromiseArray, tryConvertToPromise, INTERNAL) {
- var util = require("./util.js");
- var canEvaluate = util.canEvaluate;
- var tryCatch = util.tryCatch;
- var errorObj = util.errorObj;
- var reject;
- if (!false) {
- if (canEvaluate) {
- var thenCallback = function(i) {
- return new Function("value", "holder", " \n\
- 'use strict'; \n\
- holder.pIndex = value; \n\
- holder.checkFulfillment(this); \n\
- ".replace(/Index/g, i));
- };
- var caller = function(count) {
- var values = [];
- for (var i = 1; i <= count; ++i) values.push("holder.p" + i);
- return new Function("holder", " \n\
- 'use strict'; \n\
- var callback = holder.fn; \n\
- return callback(values); \n\
- ".replace(/values/g, values.join(", ")));
- };
- var thenCallbacks = [];
- var callers = [undefined];
- for (var i = 1; i <= 5; ++i) {
- thenCallbacks.push(thenCallback(i));
- callers.push(caller(i));
- }
- var Holder = function(total, fn) {
- this.p1 = this.p2 = this.p3 = this.p4 = this.p5 = null;
- this.fn = fn;
- this.total = total;
- this.now = 0;
- };
- Holder.prototype.callers = callers;
- Holder.prototype.checkFulfillment = function(promise) {
- var now = this.now;
- now++;
- var total = this.total;
- if (now >= total) {
- var handler = this.callers[total];
- promise._pushContext();
- var ret = tryCatch(handler)(this);
- promise._popContext();
- if (ret === errorObj) {
- promise._rejectCallback(ret.e, false, true);
- } else {
- promise._resolveCallback(ret);
- }
- } else {
- this.now = now;
- }
- };
- var reject = function (reason) {
- this._reject(reason);
- };
- }
- }
- Promise.join = function () {
- var last = arguments.length - 1;
- var fn;
- if (last > 0 && typeof arguments[last] === "function") {
- fn = arguments[last];
- if (!false) {
- if (last < 6 && canEvaluate) {
- var ret = new Promise(INTERNAL);
- ret._captureStackTrace();
- var holder = new Holder(last, fn);
- var callbacks = thenCallbacks;
- for (var i = 0; i < last; ++i) {
- var maybePromise = tryConvertToPromise(arguments[i], ret);
- if (maybePromise instanceof Promise) {
- maybePromise = maybePromise._target();
- if (maybePromise._isPending()) {
- maybePromise._then(callbacks[i], reject,
- undefined, ret, holder);
- } else if (maybePromise._isFulfilled()) {
- callbacks[i].call(ret,
- maybePromise._value(), holder);
- } else {
- ret._reject(maybePromise._reason());
- }
- } else {
- callbacks[i].call(ret, maybePromise, holder);
- }
- }
- return ret;
- }
- }
- }
- var $_len = arguments.length;var args = new Array($_len); for(var $_i = 0; $_i < $_len; ++$_i) {args[$_i] = arguments[$_i];}
- if (fn) args.pop();
- var ret = new PromiseArray(args).promise();
- return fn !== undefined ? ret.spread(fn) : ret;
- };
- };
|