Get or create a promise given a key and promise generator.
@todo Move to a utility and use in other parts of A-Frame.
519097
function PromiseCache () {
  var cache = this.cache = {};

  this.get = function (key, promiseGenerator) {
    if (key in cache) {
      return cache[key];
    }
    cache[key] = promiseGenerator();
    return cache[key];
  };
}
232425
@Override
	public <D, F, P> Promise<D, F, P> when(Promise<D, F, P> promise) {
		if (promise instanceof AndroidDeferredObject) {
			return promise;
		}
		return new AndroidDeferredObject<D, F, P>(promise).promise();
	}
51568
function push(value, key) {
        value = fn(value, key);
        if (Q.isPromise(value)) return value.then(function(v) {
            newArray.push(v);
        });
        newArray.push(value);
    }
184297
function coerce(promise) {
        var deferred = defer();
        nextTick(function () {
            try {
                promise.then(deferred.resolve, deferred.reject, deferred.notify);
            } catch (exception) {
                deferred.reject(exception);
            }
        });
        return deferred.promise;
    }
110940
function chain(promise,
               onResolve = idResolveHandler,
               onReject = idRejectHandler) {
  var deferred = getDeferred(promise.constructor);
  switch (promise.status_) {
    case undefined:
      throw TypeError;
    case 0:
      promise.onResolve_.push(onResolve, deferred);
      promise.onReject_.push(onReject, deferred);
      break;
    case +1:
      promiseEnqueue(promise.value_, [onResolve, deferred]);
      break;
    case -1:
      promiseEnqueue(promise.value_, [onReject, deferred]);
      break;
  }
  return deferred.promise;
}
502257
function co(gen) {
  var ctx = this;
  var args = slice.call(arguments, 1)

  // we wrap everything in a promise to avoid promise chaining,
  // which leads to memory leak errors.
  // see https://github.com/tj/co/issues/180
  return new Promise(function(resolve, reject) {
    if (typeof gen === 'function') gen = gen.apply(ctx, args);
    if (!gen || typeof gen.next !== 'function') return resolve(gen);

    onFulfilled();

    /**
     * @param {Mixed} res
     * @return {Promise}
     * @api private
     */

    function onFulfilled(res) {
      var ret;
      try {
        ret = gen.next(res);
      } catch (e) {
        return reject(e);
      }
      next(ret);
    }

    /**
     * @param {Error} err
     * @return {Promise}
     * @api private
     */

    function onRejected(err) {
      var ret;
      try {
        ret = gen.throw(err);
      } catch (e) {
        return reject(e);
      }
      next(ret);
    }

    /**
     * Get the next value in the generator,
     * return a promise.
     *
     * @param {Object} ret
     * @return {Promise}
     * @api private
     */

    function next(ret) {
      if (ret.done) return resolve(ret.value);
      var value = toPromise.call(ctx, ret.value);
      if (value && isPromise(value)) return value.then(onFulfilled, onRejected);
      return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, '
        + 'but the following object was passed: "' + String(ret.value) + '"'));
    }
  });
}
511914
function promisesWrapper(func) {
    return (options, callback) => {
        // options is an optional argument
        if (typeof options === 'function') {
            callback = options;
            options = undefined;
        }
        options = options || {};
        // just call the function otherwise wrap a promise around its execution
        if (typeof callback === 'function') {
            func(options, callback);
            return undefined;
        } else {
            return new Promise((fulfill, reject) => {
                func(options, (err, result) => {
                    if (err) {
                        reject(err);
                    } else {
                        fulfill(result);
                    }
                });
            });
        }
    };
}
954890
function promisedRequire (name, retries=0) {
  if (Array.isArray(name)) {
    return Promise.all(name.map((n) => {
      return promisedRequire(n);
    }));
  }
  return new Promise(function (resolve, reject) {
    global.requirejs([name], (result) => {
      resolve(result);

    }, (err) => {
      var failedId = err.requireModules && err.requireModules[0];
      if (failedId === name) {
        global.requirejs.undef(name);
        let query = `script[data-requirecontext][data-requiremodule="${name}"]`;
        let script = document.querySelector(query);
        if (script) {
          script.parentNode.removeChild(script);
        }
        if (retries < 1) {
          reject(err);
        } else {
          retries -= 1;
          promisedRequire(name, retries).then(resolve, reject);
        }
      }
    });
  });
}
1149517
function promiseify(method, ctx) {

  // check first
  if (typeof method !== 'function') {
    throw new TypeError(String(method) + ' is not a function');
  }

  return function() {

    // runtime args
    var args = [].slice.call(arguments);

    // runtime this
    ctx = ctx || this;

    return new Promise(function(resolve, reject) {

      args.push(function(err) {
        if (err) {
          return reject(err);
        }
        var arg = [].slice.call(arguments);
        if (arg.length === 2) {
          resolve.call(this, arg[1]);
        } else {
          resolve.call(this, arg.slice(1));
        }
      });

      try {
        method.apply(ctx, args);
      } catch (err) {
        reject(err);
      }
    });
  };
}
306154
function (callback) {
        return new Promise((resolve, reject) => {
          this.end(result => {
            try {
              resolve(callback(result))
            } catch (err) {
              reject(err)
            }
          })
        })
      }
580730
function get(key){
      if(isObject(key)){
        if(!isExtensible(key))return frozenStore(this).get(key);
        if(has(key, WEAK))return key[WEAK][this._i];
      }
    }
829421
def build_promise(depending_job, param_key)
      Promise.new.tap do |promise|
        promise.assign_attributes(job_id: job.id, hash_field: param_key, result_key: result_key)
        depending_job.promise_ids << promise.id
      end
    end
289043
function (key, value, dict) {
	    	var deferred = $q.defer();
	    	var promise = deferred.promise;
            
            function ok(value){
                deferred.resolve(value);
            }
            
            function errorCallback(error){
                deferred.reject(new Error(error));
            }
            
            if($window.plugins){
                var storeResult;
                if(arguments.length === 3){
                    storeResult = $window.plugins.appPreferences.store(dict, key, value);
                } else {
                    storeResult = $window.plugins.appPreferences.store(key, value);
                }
                
                storeResult.then(ok, errorCallback);
            } else {
                deferred.reject(new Error(this.pluginNotEnabledMessage));
            }
            
	    	this.decoratePromise(promise);
	    	return promise;
	    }
628892
function () {
	    	var deferred = $q.defer();
	    	var promise = deferred.promise;
            
            function ok(value){
                deferred.resolve(value);
            }
            
            function errorCallback(error){
                deferred.reject(new Error(error));
            }
            
            if($window.plugins){
                $window.plugins.appPreferences.show()
                    .then(ok, errorCallback);
            } else {
                deferred.reject(new Error(this.pluginNotEnabledMessage));
            }
	    	
	    	this.decoratePromise(promise);
	    	return promise;
	    }
1335006
@Override
	public Promise get(String key) {
		if (status.get() == STATUS_CONNECTED) {
			try {
				return client.get(key).then(in -> {
					if (in != null) {
						byte[] source = in.asBytes();
						if (source != null) {
							try {
								Tree root = serializer.read(source);
								Tree content = root.get(CONTENT);
								if (content != null) {
									return content;
								}
								return root;
							} catch (Exception cause) {
								logger.warn("Unable to deserialize cached data!", cause);
							}
						}
					}
					return Promise.resolve((Object) null);
				});
			} catch (Exception cause) {
				logger.warn("Unable to get data from Redis!", cause);
			}
		}
		return Promise.resolve((Object) null);
	}
785099
function (providerConstr, args) {
    return new Promise(function (resolve) {
        var instance = new providerConstr();
        addInstanceToStore(args, instance);
        createGetData(instance, args.caller);
        if (args.hasOwnProperty("handler")) {
            instance.setHandler(args.handler);
        }
        if (args.hasOwnProperty("resources")) {
            instance.setResources(args.resources);
        }
        instance.fetchData(resolve, args.caller);
    });
}
1128432
public Promise promise(final String name) {
    final Promise.Deferred dfd = Deferred();

    // This is the unique instance of the resolve function which will be added to each element.
    final Function resolve = new Function() {
      // Because it is an inner function, the counter cannot final outside the function
      int count = 1;
      // Inner functions don't have constructors, we use a block to initialize it
      {
        for (Element elem : elements()) {
          // Add this resolve function only to those elements with active queue
          if (queue(elem, name, null) != null) {
            emptyHooks(elem, name).add(this);
            count++;
          }
        }
      }

      public void f() {
        if (--count == 0) {
          dfd.resolve(QueuePlugin.this);
        }
      }
    };

    // Run the function and resolve it in case there are not elements with active queue
    resolve.f(this, name);

    return dfd.promise();
  }