How to use the promise.defer function in promise

To help you get started, we’ve selected a few promise examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github joewalker / devtools.html / client / netmonitor / har / har-collector.js View on Github external
waitForTimeout: function() {
    // The auto-export is not done if the timeout is set to zero (or less).
    // This is useful in cases where the export is done manually through
    // API exposed to the content.
    let timeout = Services.prefs.getIntPref(
      "devtools.netmonitor.har.pageLoadedTimeout");

    trace.log("HarCollector.waitForTimeout; " + timeout);

    this.pageLoadDeferred = defer();

    if (timeout <= 0) {
      this.pageLoadDeferred.resolve();
      return this.pageLoadDeferred.promise;
    }

    this.pageLoadTimeout = setTimeout(this.onPageLoadTimeout, timeout);

    return this.pageLoadDeferred.promise;
  },
github persvr / pintura / lib / jsgi / comet.js View on Github external
exports.Broadcaster = function(path, subscriptionApp, nextApp){
	if(request.pathInfo !== path){
		return nextApp(request);
	}
	var clientConnection = request.clientConnection;
	clientConnection.previous && (clientConnection.previous.next = clientConnection.next);
	clientConnection.next && (clientConnection.next.previous = clientConnection.previous);
	if(!clientConnection){
		clientConnection = request.clientConnection = [];
	}
	subscriptionApp(request); // ignore the response
	if(clientConnection.length){
		return sendAllMessages();
	}else{
		var deferred = defer(function(){
			clientConnection.onClose && clientConnection.onClose();
		});
		clientConnection.onMessages = function(message){
			clientConnection.lastTouched = new Date().getTime();
			if(mostRecentlyUsed){
				mostRecentlyUsed.next = clientConnection;
				clientConnection.previous = mostRecentlyUsed;
			} 
			mostRecentlyUsed = clientConnection;
			delete clientConnection.onMessages;
			// TODO: maybe queue this up for the next event turn
			deferred.resolve(drain());
		};
		return deferred.promise;
	}
	function sendAllMessages(){
github kriskowal / narwhal-lib / tests / promise.js View on Github external
exports["test reject"] = function () {

    var deferred = promiseModule.defer();
    var promise = deferred.promise;
    var reject = deferred.reject;

    var ok, error;
    promiseModule.when(promise, function (value) {
        ok = value;
    }, function (exception) {
        error = exception;
    });

    reject(new Error("result"));

    processEvents();
    assert.ok(!ok);
    assert.ok(error);
github kriskowal / narwhal-lib / tests / promise.js View on Github external
actions[i] = function(value){
            var deferred = promiseModule.defer();
            deferred.resolve(value + 1);
            return deferred.promise;
        }
    }
github kriskowal / narwhal-lib / tests / promise.js View on Github external
exports["test ref_send API resolve->when"] = function () {

    var deferred = promiseModule.defer();
    var promise = deferred.promise;
    var resolve = deferred.resolve;

    resolve("result");

    var ok, error;
    promiseModule.when(promise, function (value) {
        ok = value;
    }, function (exception) {
        error = exception;
    });
    processEvents();

    assert.ok(ok);
    assert.ok(!error);
github steelbrain / pundle / packages / pundle-core / src / master / index.js View on Github external
async _queuedProcess(callback: (worker: WorkerDelegate) => Promise): Promise {
    const currentWorker = this.transformWorkers.find(worker => worker.busyProcessing === 0)

    let promise
    if (currentWorker) {
      promise = callback(currentWorker)
    } else {
      const { promise: deferredPromise, resolve, reject } = promiseDefer()
      this.workersQueue.push(worker => {
        callback(worker).then(resolve, reject)
      })
      promise = deferredPromise
    }

    return promise
  }
  async initialize() {
github montagejs / montage / browser.js View on Github external
Require.read = function (url) {

    var request = new XMLHttpRequest();
    var response = Promise.defer();

    function onload() {
        if (xhrSuccess(request)) {
            response.resolve(request.responseText);
        } else {
            onerror();
        }
    }

    function onerror() {
        response.reject(new Error("Can't XHR " + JSON.stringify(url)));
    }

    try {
        request.open(GET, url, true);
        if (request.overrideMimeType) {