API Docs for:

Class Parse.Promise

A Promise is returned by async methods as a hook to provide callbacks to be called when the async task is fulfilled.

Typical usage would be like:

   query.find().then(function(results) {
     results[0].set("foo", "bar");
     return results[0].saveAsync();
   }).then(function(result) {
     console.log("Updated " + result.id);
   });

Parse.Promise( )
_continueWhile( predicate, asyncFunction )

Runs the given asyncFunction repeatedly, as long as the predicate function returns a truthy value. Stops repeating if asyncFunction returns a rejected promise.

Parameters:
  • predicate <Function>

    should return false when ready to stop.

  • asyncFunction <Function>

    should return a Promise.

_continueWith( continuation )

Adds a callback function that should be called regardless of whether this promise failed or succeeded. The callback will be given either the array of results for its first argument, or the error as its second, depending on whether this Promise was rejected or resolved. Returns a new Promise, like "then" would.

Parameters:
  • continuation <Function>

    the callback.

Parse.Promise _thenRunCallbacks( optionsOrCallback, model )

Run the given callbacks after this promise is fulfilled.

Parameters:
  • optionsOrCallback <Object>

    A Backbone-style options callback, or a callback function. If this is an options object and contains a "model" attributes, that will be passed to error callbacks as the first argument.

  • model <Object>

    If truthy, this will be passed as the first result of error callbacks. This is for Backbone-compatability.


Returns: <Parse.Promise>

A promise that will be resolved after the callbacks are run, with the same result as this.

always( )

Add handlers to be called when the promise is either resolved or rejected

Parse.Promise as( value )

Returns a new promise that is resolved with a given value.

Parameters:
  • value <Object>

    The value to resolve the promise with


Returns: <Parse.Promise>

the new promise.

done( )

Add handlers to be called when the Promise object is resolved

Parse.Promise error( error )

Returns a new promise that is rejected with a given error.

Parameters:
  • error <Object>

    The error to reject the promise with


Returns: <Parse.Promise>

the new promise.

fail( )

Add handlers to be called when the Promise object is rejected

Boolean is( promise )

Returns true iff the given object fulfils the Promise interface.

Parameters:
  • promise <Object>

    The object to test


Returns: <Boolean>
reject( error )

Marks this promise as fulfilled, firing any callbacks waiting on it.

Parameters:
  • error <Object>

    the error to pass to the callbacks.

resolve( result )

Marks this promise as fulfilled, firing any callbacks waiting on it.

Parameters:
  • result <Object>

    the result to pass to the callbacks.

Parse.Promise then( resolvedCallback, rejectedCallback )

Adds callbacks to be called when this promise is fulfilled. Returns a new Promise that will be fulfilled when the callback is complete. It allows chaining. If the callback itself returns a Promise, then the one returned by "then" will not be fulfilled until that one returned by the callback is fulfilled.

Parameters:
  • resolvedCallback <Function>

    Function that is called when this Promise is resolved. Once the callback is complete, then the Promise returned by "then" will also be fulfilled.

  • rejectedCallback <Function>

    Function that is called when this Promise is rejected with an error. Once the callback is complete, then the promise returned by "then" with be resolved successfully. If rejectedCallback is null, or it returns a rejected Promise, then the Promise returned by "then" will be rejected with that error.


Returns: <Parse.Promise>

A new Promise that will be fulfilled after this Promise is fulfilled and either callback has completed. If the callback returned a Promise, then this Promise will not be fulfilled until that one is.

Parse.Promise when( promises )

Returns a new promise that is fulfilled when all of the input promises are resolved. If any promise in the list fails, then the returned promise will be rejected with an array containing the error from each promise. If they all succeed, then the returned promise will succeed, with the results being the results of all the input promises. For example:

var p1 = Parse.Promise.as(1);
var p2 = Parse.Promise.as(2);
var p3 = Parse.Promise.as(3);

Parse.Promise.when(p1, p2, p3).then(function(r1, r2, r3) { console.log(r1); // prints 1 console.log(r2); // prints 2 console.log(r3); // prints 3 });

The input promises can also be specified as an array:

var promises = [p1, p2, p3];
Parse.Promise.when(promises).then(function(results) {
console.log(results);  // prints [1,2,3]
});

Parameters:
  • promises <Array>

    a list of promises to wait for.


Returns: <Parse.Promise>

the new promise.