How to use the when/function.call 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 spion / async-compare / examples / promises.js View on Github external
module.exports = function upload(stream, idOrPath, tag, done) {
    var blob = blobManager.create(account);
    var tx = db.begin();
    var blobIdP = blob.put(stream); 
    var fileP = self.byUuidOrPath(idOrPath).get();
    var previousIdP = p.ternary(fileP, p.get(fileP, 'version'), null);
    var versionP = p.allObject({
        userAccountId: userAccount.id,
        date: new Date(),
        blobId: blobIdP,
        creatorId: userAccount.id,
        previousId: previousIdP,
    });
    versionP = p.set(versionP, p.allObject({
        id: fn.call(Version.createHash, versionP)
    }));
    // Even if Version.insert has been lifted, it returns a promise and 
    // therefore we cannot call execWithin. We have to wait for the promise 
    // to resolve
    var versionInsert = p.eventuallyCall(
        Version.insert(versionP), 'execWithin', tx);
    var versionIdP = p.get(versionP, 'id');
    var fileIdP = p.if (p.not(fileP), function () {
        var splitPath = idOrPath.split('/');
        var fileName = splitPath[splitPath.length - 1];
        var newId = uuid.v1();
        p.eventuallyCall(self.createQuery(idOrPath, p.allObject({
            id: newId,
            userAccountId: userAccount.id,
            name: fileName,
            version: versionIdP
github aerisweather / aerisjs / tasks / jasmine-amd / jasmine-amd.js View on Github external
preserveSpecRunner: true,
      outDir: 'tests'
    });
    var target = this.target;
    var tempFiles = [];
    var specRunnerPath = path.join(options.outDir, 'specrunner-tmp.html');

    // Use specs argument, if provided
    if (opt_specs) {
      options.specs = opt_specs.split(',');

      // Do not exclude any specs, if specs argument is passed
      options.exclude = [];
    }

    fn.call(setup).
      then(run).
      then(done).
      catch(function(err) {
        var errObj = err instanceof Error ? err : new Error(err);
        done(errObj);
      }).
      finally(tearDown);


    function setup() {
      var specs = grunt.file.expand(options.specs);
      var excludedSpecs = grunt.file.expand(options.exclude);
      grunt.verbose.write('Running specs: ' + JSON.stringify(specs, null, 2) + '\n');
      grunt.verbose.write('Excluding specs: ' + JSON.stringify(excludedSpecs, null, 2) + '\n');
      createTempFile(specRunnerPath, generateSpecRunner(options));
    }
github petkaantonov / bluebird / benchmark / async-compare / examples-extra / promises.js View on Github external
module.exports = function upload(stream, idOrPath, tag, done) {
    var blob = blobManager.create(account);
    var tx = db.begin();
    var blobIdP = blob.put(stream); 
    var fileP = self.byUuidOrPath(idOrPath).get();
    var previousIdP = p.ternary(fileP, p.get(fileP, 'version'), null);
    var versionP = p.allObject({
        userAccountId: userAccount.id,
        date: new Date(),
        blobId: blobIdP,
        creatorId: userAccount.id,
        previousId: previousIdP,
    });
    versionP = p.set(versionP, p.allObject({
        id: fn.call(Version.createHash, versionP)
    }));
    // Even if Version.insert has been lifted, it returns a promise and 
    // therefore we cannot call execWithin. We have to wait for the promise 
    // to resolve
    var versionInsert = p.eventuallyCall(
        Version.insert(versionP), 'execWithin', tx);
    var versionIdP = p.get(versionP, 'id');
    var fileIdP = p.if (p.not(fileP), function () {
        var splitPath = idOrPath.split('/');
        var fileName = splitPath[splitPath.length - 1];
        var newId = uuid.v1();
        p.eventuallyCall(self.createQuery(idOrPath, p.allObject({
            id: newId,
            userAccountId: userAccount.id,
            name: fileName,
            version: versionIdP
github exis-io / Exis / js / jsRiffle / src / session.js View on Github external
if (kwargs_len) {
                     progress_msg.push(kwargs);
                  }
               }
               self._send_wamp(progress_msg);
            }
         };

         var cd = new Invocation(details.caller, progress, details.procedure);

         // We use the following whenjs call wrapper, which automatically
         // wraps a plain, non-promise value in a (immediately resolved) promise
         //
         // See: https://github.com/cujojs/when/blob/master/docs/api.md#fncall
         //
         when_fn.call(endpoint, args, kwargs, cd).then(

            function (res) {
               // construct YIELD message
               // FIXME: Options
               //
               var reply = [MSG_TYPE.YIELD, request, {}];

               if (res instanceof Result) {
                  var kwargs_len = Object.keys(res.kwargs).length;
                  if (res.args.length || kwargs_len) {
                     reply.push(res.args);
                     if (kwargs_len) {
                        reply.push(res.kwargs);
                     }
                  }
               } else {
github crossbario / autobahn-js / lib / session.js View on Github external
// we want to provide the regitration procedure to the handler and may
         // need to get this from the registration object attached to the registration
         // since for non-pattern registrations this is not sent over the wire
         var cd = new Invocation(details.procedure || reg.procedure,
                                 progress,
                                 details.caller,
                                 details.caller_authid,
                                 details.caller_authrole
                      );

         // We use the following whenjs call wrapper, which automatically
         // wraps a plain, non-promise value in a (immediately resolved) promise
         //
         // See: https://github.com/cujojs/when/blob/master/docs/api.md#fncall
         //
         when_fn.call(reg.endpoint, args, kwargs, cd).then(

            function (res) {
               // construct YIELD message
               // FIXME: Options
               //
               var reply = [MSG_TYPE.YIELD, request, {}];

               if (res instanceof Result) {
                  var kwargs_len = Object.keys(res.kwargs).length;
                  if (res.args.length || kwargs_len) {
                     reply.push(res.args);
                     if (kwargs_len) {
                        reply.push(res.kwargs);
                     }
                  }
               } else {
github rkaw92 / esdf / LogicSaga.js View on Github external
LogicSaga.prototype.handleTimer = function handleTimer(timerID, actualTriggerTime, deps){
	var self = this;
	// NOTE: Dynamic dispatch! Watch out for name collisions!
	var timerType = this._activeTimers[timerID];
	var timerHandlerName = 'handle' + timerType + 'Timer';
	this._finishTimer(timerID, actualTriggerTime);
	if(typeof(this[timerHandlerName]) === 'function'){
		return whenFunctions.call(this[timerHandlerName].bind(this), timerID, actualTriggerTime, deps);
	}
	else{
		throw new LogicSagaTimerHandlerMissingError(timerID, timerType);
	}
};
github jsantell / poet / lib / poet / methods.js View on Github external
var post = utils.createPost(file, options).then(function(post) {
        var viewOpts = {
          source: '',
          filename: file,
          locals: poet.app ? poet.app.locals : {}
        };
        return when.join(fn.call(template, _.extend({}, viewOpts, { source: post.content })),
                         fn.call(template, _.extend({}, viewOpts, { source: post.preview })))
               .then(function(contents) {
                 post.content = contents[0];
                 post.preview = contents[1] + options.readMoreLink(post);
                 return post;
               }, function(err) {
                 console.error('Unable to parse file ' + file + ': ' + err);
                 if (process.env.NODE_ENV === 'production') {
                   return err;
                 }
                 post.content = post.preview = '<pre style="font-family: monospace">' + err + '</pre>';
                 return post;
               });
      }).then(function(post) {
        if (!(post instanceof Error))
github jsantell / poet / lib / poet / methods.js View on Github external
var post = utils.createPost(file, options).then(function(post) {
        var viewOpts = {
          source: '',
          filename: file,
          locals: poet.app ? poet.app.locals : {}
        };
        return when.join(fn.call(template, _.extend({}, viewOpts, { source: post.content })),
                         fn.call(template, _.extend({}, viewOpts, { source: post.preview })))
               .then(function(contents) {
                 post.content = contents[0];
                 post.preview = contents[1] + options.readMoreLink(post);
                 return post;
               }, function(err) {
                 console.error('Unable to parse file ' + file + ': ' + err);
                 if (process.env.NODE_ENV === 'production') {
                   return err;
                 }
                 post.content = post.preview = '<pre style="font-family: monospace">' + err + '</pre>';
                 return post;
               });
      }).then(function(post) {
        if (!(post instanceof Error))
github exis-io / Exis / js / jsRiffle / src / session.js View on Github external
var details = msg[1];
            var reason = msg[2];

            if (self.onleave) {
               self.onleave(reason, details);
            }

         } else if (msg_type === MSG_TYPE.CHALLENGE) {

            if (self._onchallenge) {

               var method = msg[1];
               var extra = msg[2];

               when_fn.call(self._onchallenge, self, method, extra).then(
                  function (signature) {
                     var msg = [MSG_TYPE.AUTHENTICATE, signature, {}];
                     self._send_wamp(msg);
                  },
                  function (err) {
                     log.debug("onchallenge() raised:", err);

                     var msg = [MSG_TYPE.ABORT, {message: "sorry, I cannot authenticate (onchallenge handler raised an exception)"}, "wamp.error.cannot_authenticate"];
                     self._send_wamp(msg);
                     self._socket.close(1000);
                  }
               );
            } else {
               log.debug("received WAMP challenge, but no onchallenge() handler set");

               var msg = [MSG_TYPE.ABORT, {message: "sorry, I cannot authenticate (no onchallenge handler set)"}, "wamp.error.cannot_authenticate"];
github crossbario / autobahn-js / lib / session.js View on Github external
var details = msg[1];
            var reason = msg[2];

            if (self.onleave) {
               self.onleave(reason, details);
            }

         } else if (msg_type === MSG_TYPE.CHALLENGE) {

            if (self._onchallenge) {

               var method = msg[1];
               var extra = msg[2];

               when_fn.call(self._onchallenge, self, method, extra).then(
                  function (signature) {

                     if(typeof signature === "string"){
                        var msg = [MSG_TYPE.AUTHENTICATE, signature, {}];
                     } else if (typeof signature === "object") {
                        var signatureString = signature[0];
                        var authExtra = signature[1];

                        var msg = [MSG_TYPE.AUTHENTICATE, signatureString, authExtra];
                     }

                     self._send_wamp(msg);
                  },
                  function (err) {
                      util.handle_error(self._on_user_error, err, "onchallenge() raised: ");
                      var msg = [MSG_TYPE.ABORT, {message: "sorry, I cannot authenticate (onchallenge handler raised an exception)"}, "wamp.error.cannot_authenticate"];