How to use the when.defer function in when

To help you get started, we’ve selected a few when 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 bocoup / mobilevis / api / app / models / submission.js View on Github external
add : function(props) {

    var self = this;
    var def = when.defer();
    var checkExistance = when.defer();
    var tagDef = when.defer();

    try {

      // if we have an original url, make sure it's a url
      if (props.original_url && props.original_url.length) {
        assert(Validator.isURL(props.original_url), "original_url isn't a URL");

        // check if this already exists, based on URL. if it does, exit.
        self.forge({ original_url : props.original_url }).fetch().then(function(submission) {
          console.log(submission);
          if (typeof submission !== "undefined" && submission !== null) {
            checkExistance.reject("This example already exists. Id " + submission.id);
          } else {
            checkExistance.resolve();
          }
        });
github crossbario / autobahn-js / work / session.js View on Github external
Session.prototype.register = function (endpoint, procedure, options) {
   var self = this;

   // create an remember new REGISTER request
   //
   var request = newid();
   var d = when.defer();
   self._register_reqs[request] = [d, endpoint, options];

   // construct REGISTER message
   //
   var msg = [MSG_TYPE.REGISTER, request];
   if (options) {
      msg.push(options);
   } else {
      msg.push({});
   }
   msg.push(procedure);

   // send WAMP message
   //
   self._send_wamp(msg);
github nfe / client-nodejs / lib / BaseResource.js View on Github external
createDeferred: function(callback) {
      var deferred = when.defer();

      if (callback) {
        // Callback, if provided, is a simply translated to Promise'esque:
        // (Ensure callback is called outside of promise stack)
        deferred.promise.then(function(res) {
          setTimeout(function(){ callback(null, res) }, 0);
        }, function(err) {
          setTimeout(function(){ callback(err, null); }, 0);
        });
      }

      return deferred;
  },
github zeit / ncc / test / integration / when.js View on Github external
var when = require('when');

var deferred = when.defer();
github luqmaan / Instabus / src / js / models / StopDetails.js View on Github external
StopDetails.prototype.fetch = function() {
    var deferred = when.defer();
    var yqlURL = 'http://query.yahooapis.com/v1/public/yql';
    var capURL = 'https://www.capmetro.org/planner/s_nextbus2.asp?stopid=' + this.stopID() + '&route=' + this.routeID();
    var params = {
        q: 'select * from xml where url="' + capURL + '"',
        format: 'json'
    };

    this.errorMsg(null);

    function retryAtMost(maxRetries) {
        requests.get(yqlURL, params)
            .then(this.parseResponse.bind(this))
            .tap(function(Runs) {
                if (Runs.length > 0) {
                    this.tripCollection = new TripCollection(this.stopID(), this.routeID(), Runs);
                }
github OptimalBits / livef1 / livef1.js View on Github external
function readStream(stream, numBytes){
  var deferred = when.defer();
  var data = stream.read(numBytes);
  if(data){
    deferred.resolve(data);
  }else{
    stream.once && stream.once('readable', function(){
      var promise = readStream(stream, numBytes);
      promise.then(function(data){
        deferred.resolve(data);
      });
    });
  }
  return deferred.promise;
}
github vizorvr / patches / lib / cloudStorage.js View on Github external
readString(path) {
    const stream = this.read(path)
    const dfd = when.defer()
    let buf = ''

    stream.on('data', d => buf += d.toString())
    stream.on('error', err => dfd.reject(err))
    stream.on('end', () => dfd.resolve(buf))

    return dfd.promise
  }
github votinginfoproject / Metis / feed-processor / rule / impl / uniqueIdToo.js View on Github external
/**
 * Created by nboseman on 2/11/14.
 */

var schemas = require('../../../dao/schemas');
var mongoose = require('mongoose');
var ruleViolation = require('../ruleviolation');
var when = require('when');
var deferred = when.defer();

var rule = null;

var evaluateUniqueId = function(feedId, constraintSet, ruleDefinition){
  var when = require('when');
  rule = ruleDefinition;

  var finds = [];
    constraintSet.entity.forEach(function(model) {
      Model = mongoose.model(model);
      finds.push(Model.find({ '_feed': feedId },{'elementId':1,'_feed':1}).exec());
    }, this);

    when.all(finds)
      .then(processQueryResults, errorHandler);
github LeanKit-Labs / seriate / src / sqlContext.js View on Github external
then: function( success, failure ) {
			var deferred = when.defer();
			function onSuccess( result ) {
				deferred.resolve( result );
			}
			function onFailure( error ) {
				deferred.reject( error );
			}

			this.end( onSuccess );
			this.error( onFailure );

			return deferred.promise
				.then( success, failure );
		},
github BrandonCookeDev / smashgg.js / lib / Event.js View on Github external
static getEvent(eventName, tournamentName, options={}){
		let deferred = when.defer();
		try{
			let E = new Event(eventName, tournamentName, options);
			E.on('ready', function(){
				deferred.resolve(E);
			});
			E.on('error', function(e){
				log.error('getEvent error: %s', e);
				deferred.reject(e);
			});
		} catch(e){
			log.error('getEvent error: %s', e);
			deferred.reject(e);
		}
		return deferred.promise;
	}