back to Overview

promise.then()

Registers two callbacks that will be invoked when the Promise's asynchronous operation finished successfully (onSuccess) or an error occurred (onError).
Web module only.

Syntax Variants

promise.then()
promise.then(onSuccess)
promise.then(onSuccess, onError)

Parameters

onSuccess (optional)
a callback function to be called when the operation has been completed successfully. The exact arguments it receives depend on the operation. If the function returns a Promise, that Promise will be evaluated to determine the state of the promise returned by then(). If it returns any other value, the returned Promise will also succeed. If the function throws an error, the returned Promise will be in error state. Pass null or undefined if you do not need the success handler.
onError (optional)
a callback function to be called when the operation failed. The exact arguments it receives depend on the operation. If the function returns a Promise, that promise will be evaluated to determine the state of the Promise returned by then(). If it returns anything else, the returned Promise will have success status. If the function throws an error, the returned Promise will be in the error state. You can pass null or undefined if you do not need the error handler.
(return value)
a new Promise object. If you specified a callback for success or error, the new Promises's state will be determined by that callback if it is called. If no callback has been provided and the original Promise changes to that state, the new Promise will change to that state as well.

Description

Registers two callbacks that will be invoked when the Promise's asynchronous operation finished successfully (onSuccess) or an error occurred (onError). The callbacks will be called after then() returned, from the browser's event loop. You can chain then() invocations, as then() returns another Promise object that you can attach to.

The full distribution of Minified implements the Promises/A+ specification, allowing interoperability with other Promises frameworks.

Note: If you use the Web module, you will get a simplified Promises implementation that cuts some corners. The most notable difference is that when a then() handler throws an exception, this will not be caught and the promise returned by then will not be automatically rejected.

Example

Simple handler for an HTTP request. Handles only success and ignores errors.

$.request('get', '/weather.html')
    .then(function(txt) {
       alert('Got response!');
    });

Example

Including an error handler.

$.request('get', '/weather.html')
    .then(function(txt) {
       alert('Got response!');
    }, function(err) {
       alert('Error!');
    }));

Example

Chained handler.

$.request('get', '/weather.do')
    .then(function(txt) {
       showWeather(txt);
    }
    .then(function() {
       return $.request('get', '/traffic.do');
    }
    .then(function(txt) {
       showTraffic(txt);
    }
    .then(function() {
       alert('All result displayed');
    }, function() {
       alert('An error occurred');
    });

Comments

comments powered by Disqus

back to Overview

Functions