Defers a function's execution to the next iteration in the event loop

@arg {function} cb the callback function
@returns {Object} the callback's return value
186563
function defer(cb) {
    if (!defer.nextLoop) {
        var ntv = typeof setImmediate === 'function';
        defer.nextLoop = ntv ? setImmediate : function setImmediateShim(cb) {
            if (defer.obj) {
                if (!defer.cbs.length) {
                    defer.obj.setAttribute('cnt', 'inc');
                }
                defer.cbs.push(cb);
            } else {
                setTimeout(cb, 0);
            }
        };
        if (!ntv && typeof MutationObserver === 'object' && typeof document === 'object') {
            defer.cbs = [];
            defer.obj = document.createElement('div');
            defer.ob = new MutationObserver(function mutations() {
                for (var cbl = defer.cbs.slice(), i = defer.cbs.length = 0, l = cbl.length; i < l; i++) {
                    cbl[i]();
                }
            }).observe(defer.obj, { attributes: true });
        }
    }
    return defer.nextLoop.call(null, cb);
}
519564
function nextup(inst, orig, argv, event, cb) {
    return function nextupCallback(err, data) {
        if (err) return cb(err, null);
        if (_isQuoted(data)) return cb(null, _unquote(data));
        if (_isValue(data)) return cb(null, _entityToValue(data, inst.library));
        // Remove any nulls or undefineds from the list
        var compact = _compactList(data);
        // Allow the programmer to opt out for _minor_ perf gains
        if (inst.options.doIrreducibleListCheck) {
            // If no change, we'll keep looping forever, so just return
            if (IsEqual(orig, compact)) {
                Console.printWarning(inst, 'Detected irreducible list; exiting...');
                return cb(null, compact);
            }
        }
        return exec(inst, compact, argv, event, cb);
    };
}
646774
def _deferGenerator(g, deferred=None):
    
    result = None
    while 1:
        if deferred is None:
            deferred = defer.Deferred()
        try:
            result = g.next()
        except StopIteration:
            deferred.callback(result)
            return deferred
        except:
            deferred.errback()
            return deferred

        # Deferred.callback(Deferred) raises an error; we catch this case
        # early here and give a nicer error message to the user in case
        # they yield a Deferred. Perhaps eventually these semantics may
        # change.
        if isinstance(result, defer.Deferred):
            return defer.fail(TypeError("Yield waitForDeferred(d), not d!"))

        if isinstance(result, defer.waitForDeferred):
            waiting = [True, None]
            # Pass vars in so they don't get changed going around the loop
            def gotResult(r, waiting=waiting, result=result):
                result.result = r
                if waiting[0]:
                    waiting[0] = False
                    waiting[1] = r
                else:
                    _deferGenerator(g, deferred)
            result.d.addBoth(gotResult)
            if waiting[0]:
                # Haven't called back yet, set flag so that we get reinvoked
                # and return from the loop
                waiting[0] = False
                return deferred
            result = None
563467
function defer() {
  const def = promiseDefer();

  def.apply = function (err, result) {
    if (err) {
      def.reject(err);
    } else {
      def.resolve(result);
    }
  };

  return def;
}
481470
function (cb, expect) {
        if (_.isFunction(cb) && cb.__normalised) {
            return meetExpectations(cb, expect);
        }

        var userback, // this var will be populated and returned
            // keep a reference of all initial callbacks sent by user
            callback = (_.isFunction(cb) && cb) || (_.isFunction(cb && cb.done) && cb.done),
            callbackError = _.isFunction(cb && cb.error) && cb.error,
            callbackSuccess = _.isFunction(cb && cb.success) && cb.success;

        // create master callback that calls these user provided callbacks
        userback = _.assign(function (err) {
            // if common callback is defined, call that
            callback && callback.apply(this, arguments);

            // for special error and success, call them if they are user defined
            if (err) {
                callbackError && callbackError.apply(this, arguments);
            }
            else {
                // remove the extra error param before calling success
                callbackSuccess && callbackSuccess.apply(this, (Array.prototype.shift.call(arguments), arguments));
            }
        }, _.isPlainObject(cb) && cb, { // override error, success and done
            error: function () {
                return userback.apply(this, arguments);
            },
            success: function () {
                // inject null to arguments and call the main callback
                userback.apply(this, (Array.prototype.unshift.call(arguments, null), arguments));
            },
            done: function () {
                return userback.apply(this, arguments);
            },
            __normalised: true
        });

        return meetExpectations(userback, expect);
    }
1202101
function (cb) {
                var scope = cb.scope, callback;
                scope = scope ? scope : this;
                if (!cb.fn) {
                    callback = cb;
                } else {
                    callback = cb.fn;
                }

                if (typeof(callback) == 'string') {
                    callback = scope[callback];
                }
                return {
                    fn : callback,
                    scope : scope,
                    args : cb.args,
                    resIndex : cb.resIndex,
                    apply : cb.apply
                };
            }
839492
<R> CMACallback<R> defer(DefFunc<R> func, CMACallback<R> callback) {
    assertNotNull(callback, "callback");
    Observable.defer(func)
        .subscribeOn(Schedulers.io())
        .subscribe(
            new ActionSuccess<R>(callbackExecutor, callback),
            new ActionError(callbackExecutor, callback));
    return callback;
  }
433425
function waitFor(check, cb, params) {
    params = params || {};
    if (typeof cb === 'object') {
      params = cb;
      cb = params.done;
    }

    var milliseconds = params.interval || 30,
        j = 0;

    var i = setInterval(function() {
      if (check()) {
        clearInterval(i);
        cb(null);
      }

      if (params.timeout && params.timeout - (j * milliseconds) <= 0) {
        clearInterval(i);
        cb(new Error('timeout'));
      }

      j++;
    }, milliseconds);
  }
755522
function(callback) {
		this.promises.push(function async(done, utils) {
			callback(utils.lastReturn, done, utils);
		});
		return this;
	}
669230
function(callback) {
      var i = 0,
          keys = this.keys(),
          returned;

      for (i; i < keys.length; i++) {
        returned = callback(keys[i], this.get(keys[i]));
        if (returned === false) { return false; }
      };
    }
182520
function (a, element, event, callback) {
                var cbCheck;
                for (var i = 0, l = a.length; i < l; i = i + 1) {
                    var li = a[i];
                    if (li && this.typesUtil.isObject(li[FN]) && !li[FN].$Callback) {
                        cbCheck = ("fn" in callback) && (li[FN].fn == callback.fn);
                    } else {
                        cbCheck = (li[FN] == callback);
                    }
                    if (li && cbCheck && li[EL] == element && li[TYPE] == event) {
                        return i;
                    }
                }

                return -1;
            }
1324107
function setDeferredTimeout(deferred, delay) {
        var timer = setTimeout(function () {
            deferred.reject("timeout");
        }, delay);
        deferred.always(function () { clearTimeout(timer); });
    }
695574
function() {

    this.on = this.addEventListener = function(type, listener) {
        if (!this.events[type]) {
            this.events[type] = [];
        }

        this.events[type].push(listener);
    };

    this.off = this.removeEventListener = function (type, listener) {
        if (!!this.events[type]) {
            for(var i = 0; i < this.events[type].length; i++) {
                if (this.events[type][i] === listener) {
                    this.events[type][i] = null;
                    this.events[type].splice(i, 1);
                    return;
                }
            }
        }
    };

    this.removeAllListeners = function (type) {
        this.events[type] = [];
    };

    var emit = function(cb, event) {
        // Handle callbacks asynchronously
        setTimeout(function() {
            cb(event);
        }, 0);

    };

    this.dispatchEvent = function (event) {
        if (!!event.type) {
            var type = event.type;
            if (!!this.events[type]) {
                event.target = this;
                for (var i = 0; i < this.events[type].length && (event.returnValue === undefined || event.returnValue) ; i++) {
                    var cb = this.events[type][i];
                    // Dereference cb for async
                    emit(cb.bind(event.target), event);
                }
            }
        }
    };
}
1229634
function(err) {
        if (err) this._onError(err);
        if (this._continueErrors || !err) {
            var args = [].slice.call(arguments);
            if (this._callbacks.length > 0) {
                this._isQueueRunning = true;
                var cb = this._callbacks.shift();
                cb = cb.bind(this);
                args = args.slice(1);
                args.push(this.next);
                cb.apply(this, args);

            } else {
                this._isQueueRunning = false;
                this.start = (function() {
                    this.start = null;
                    this.next.apply(this, args);
                }).bind(this);

            }
        }
        return this;
    }
100118
function(cb) {
    cb = cb || _.noop;
    const self = this;
    self._step = 0;
    if (self._stack.length<=0) return cb();
    (function next() {
      const step = _getStep(self);
      if (!step) {
        cb();
      } else if (_.isFunction(step)) {
        step.call(self, next);
      } else if (_.isFunction(step[self._exec])) {
        step[self._exec](next);
      }
    })();
  }
516732
function Deferred(opts){
 	/**
	 * @define {object} Collection of public methods.
	 */
	var self = {};

	/**
	 * @define {object} options for the constructor 
	 */
	var opts = opts || {};

	/**
	 * @define {object} A promise object that will be returned
	 */
	var promise = {};

	/**
	 * @define {object} Event system for controlling events
	 */
	var event = Event();
	
	/**
	 * Promise then method
	 * This is for chaining promis callbacks
	 * @example promiseFunction().then(
	 * function(){ // success }, 
	 * function(){ // rejected }, 
	 * function(){ // notify } 
	 * ).then( ... );
	 * @param {function} sucess Success callback
	 * @param {function} reject Reject callback
	 * @param {function} notify notify callback
	 * @return {object} Returns the promise object
	 */
	function promiseThen(success, reject, notify){
		/**
		 * @define {object} Return a new promise
		 */
		var def = Deferred();

		/**
		 * Resolved promise
		 * @example example
		 * @param {string} A string key for the event system
		 * @param {function} A callback when event is triggered
		 * @return {object} Returns promise object
		 */
		event.on('resolve', function(){ 
			/**
			 * If the success callback returns a promise
			 * then resolve/reject/notify that returned promise
			 */
			var promise = success();
			if(!promise) return;
			promise.success(function(){
				/** handle the returned deferred object */
				def.resolve();
			});
			promise.error(function(){
				def.reject();
			});
			promise.notify(function(){
				def.notify();
			});
		});

		/**
		 * If promise is rejected/notify trigger the callback
		 */
		event.on('reject', function(){ 
			if(reject) reject();
		});

		event.on('notify', function(){ 
			if(notify) notify();
		});

		/**
		 * @return {object} Returns a promise object
		 */
		return def.promise; 
	}

	/**
	 * Promise methods
	 * @example promise.then( //new promise ).then(...)
	 * @example promise.success(...).error(...).notify(...)
	 * @param {function} callback A callback for the promise
	 * @return {object} Return the promise
	 */
	function promiseSuccess(callback){
		event.on('resolve', callback);
		return promise;
	}

	function promiseError(callback){
		event.on('reject', callback);
		return promise;
	}

	function promiseNotified(callback){
		event.on('notify', callback);
		return promise;
	}

	/**
	 * Deferred methods to trigger the promise
	 * @example def.resolve(args)
	 * @example def.reject(args)
	 * @example def.notify(args)
	 */
	function resolve(){
		var args = Array.prototype.slice.call(arguments);
		event.trigger('resolve', args);
	}

	function reject(){
		var args = Array.prototype.slice.call(arguments);
		event.trigger('reject', args);	
	}

	function notify(){
		var args = Array.prototype.slice.call(arguments);
		event.trigger('notify', args);		
	}

	/**
	 * Add the promise methods to the promise object
	 */
	promise.then = promiseThen;
	promise.success = promiseSuccess;
	promise.error = promiseError;
	promise.notify = promiseNotified;

	/**
	 * The promise object
	 * @public {object}
	 */
	self.promise = promise;

	/**
	 * Deferred public methods	
	 * @public {function}
	 */
	self.resolve = resolve;
	self.reject = reject;
	self.notify = notify;

	/**
	 * @return {object} return public methos
	 */
	return self;
}
1298718
function (objs, cb) {

      var results = []
        ,   done = false
        ,   self = this

      var updateProgress = function(obj) {
        results.push(obj)
        done = results.length === objs.length
      }

      var checkProgress = setInterval(function() {
        if (done) {
          if (cb) self.lambda(cb).call(self, results)
          clearInterval(checkProgress)
        }
      }, 200)

      for (var i = 0, l = objs.length; i < l; i++)
        this.save(objs[i], updateProgress)

      return this
    }