Intern Implicit And Explicit Promises

intern

// Intern - Implicit promises and explicit promises:

Source: http://marcoluglio.github.io/tutorialintern/

Calling any method of Command will return a new Command instance.  Multiple 
method calls will return multiple command instances.

Implicit promises with parallel commands:

var pageCommand = this.remote.get(require.toUrl('http://localhost'));

var command1 = pageCommand.findById('someId').getAttribute('for');
var command2 = pageCommand.findById('someOtherId').getAttribute('for');

The all helper will wait for all passed promises to be fulfilled before invoking 
its own "then" callbacks.  Because commands have implicit promises, we pass them 
directly to the "all" helper.  The all([...]).then callback will receive an 
array with the results of each promise.

return all([command1, command2]) // implicit promises

Explicit promises with parallel commands:

var promise = new Deferred();
var pageCommand = this.remote.get(require.toUrl('http://localhost'));

var command1 = pageCommand.findById('someId').getAttribute('for');
var command2 = pageCommand.findById('someOtherId').getAttribute('for');

all([command1, command2])
  .then(
    function(values) {
      promise.resolve();
    }
  )
  .catch(
    function(error) {
      promise.reject(error);
    }
  );

return promise;

Notice that in the above code, in the case of implicit promise, when invoking 
the "all" helper method, we do not specify a "then" callback.  The all helper 
method probably return a promise that has a "default then" callback.

Additionally, in the case of implicit promise, we return the result of the all 
helper, and in the case of explicit promise, we actually create a promise object, 
resolve or reject it, and return the promise object.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License