Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 926455 of Promise.prototype.then()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Promise/then
  • Revision title: Promise.prototype.then()
  • Revision id: 926455
  • Created:
  • Creator: DBaron
  • Is current revision? No
  • Comment better wrapping

Revision Content

{{JSRef}} {{harmony}}

The then() method returns a Promise. It takes two arguments, both are callback functions for the success and failure cases of the Promise.

Syntax

p.then(onFulfilled, onRejected);

p.then(function(value) {
   // fulfillment
  }, function(reason) {
  // rejection
});

Parameters

onFulfilled
A {{jsxref("Function")}} called when the Promise is fulfilled. This function has one argument, the fulfillment value.
onRejected
A {{jsxref("Function")}} called when the Promise is rejected. This function has one argument, the rejection reason.

Description

As the then and {{jsxref("Promise.prototype.catch()")}} methods return promises, they can be chained — an operation called composition.

Examples

Using the then method

var p1 = new Promise(function(resolve, reject) {
  resolve("Success!");
  // or
  // reject ("Error!");
});

p1.then(function(value) {
  console.log(value); // Success!
}, function(reason) {
  console.log(reason); // Error!
});

Chaining

Because the then method returns a Promise, you can easily chain then calls.

var p2 = new Promise(function(resolve, reject) {
  resolve(1);
});

p2.then(function(value) {
  console.log(value); // 1
  return value + 1;
}).then(function(value) {
  console.log(value); // 2
});

p2.then(function(value) {
  console.log(value); // 1
});

You can also use chaining to implement one function with a Promise-based API on top of another such function.

function fetch_current_data() {
  // The fetch() API returns a Promise.  This function
  // exposes a similar API, except the fulfillment
  // value of this function's Promise has had more
  // work done on it.
  return fetch("current-data.json").then((response) => {
    if (response.headers.get("content-type") != "application/json") {
      throw new TypeError();
    }
    var j = response.json();
    // maybe do something with j
    return j; // fulfillment value given to user of
              // fetch_current_data().then()
  });
}

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-promise.prototype.then', 'Promise.prototype.then')}} {{Spec2('ES6')}} Initial definition in an ECMA standard.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 32 {{CompatGeckoDesktop(29.0)}} [1] {{CompatNo}} 19 7.1
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatGeckoMobile(29.0)}} [1] {{CompatNo}} {{CompatNo}} 8 32

[1] Gecko 24 has an experimental implementation of Promise, under the initial name of Future. It got renamed to its final name in Gecko 25, but disabled by default behind the flag dom.promise.enabled. Bug 918806 enabled Promises by default in Gecko 29.

See also

  • {{jsxref("Promise")}}
  • {{jsxref("Promise.prototype.catch()")}}

Revision Source

<div>{{JSRef}} {{harmony}}</div>

<p>The <code><strong>then()</strong></code> method returns a <code>Promise</code>. It takes two arguments, both are callback functions for the success and failure cases of the <code>Promise</code>.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
<var>p.then(onFulfilled, onRejected)</var>;

p.then(function(value) {
   // fulfillment
  }, function(reason) {
  // rejection
});
</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt>onFulfilled</dt>
 <dd>A {{jsxref("Function")}} called when the <code>Promise</code> is fulfilled. This function has one argument, the fulfillment <code>value</code>.</dd>
 <dt>onRejected</dt>
 <dd>A {{jsxref("Function")}} called when the <code>Promise</code> is rejected. This function has one argument, the rejection <code>reason</code>.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>As the <code>then</code> and {{jsxref("Promise.prototype.catch()")}} methods return promises, they can be chained — an operation called <em>composition</em>.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Using_the_then_method">Using the <code>then</code> method</h3>

<pre class="brush: js">
var p1 = new Promise(function(resolve, reject) {
  resolve("Success!");
  // or
  // reject ("Error!");
});

p1.then(function(value) {
  console.log(value); // Success!
}, function(reason) {
  console.log(reason); // Error!
});
</pre>

<h3 id="Chaining">Chaining</h3>

<p>Because the <code>then</code> method returns a <code>Promise</code>, you can easily chain <code>then</code> calls.</p>

<pre class="brush: js">
var p2 = new Promise(function(resolve, reject) {
  resolve(1);
});

p2.then(function(value) {
  console.log(value); // 1
  return value + 1;
}).then(function(value) {
  console.log(value); // 2
});

p2.then(function(value) {
  console.log(value); // 1
});
</pre>

<p>You can also use chaining to implement one function with a Promise-based API on top of another such function.</p>

<pre class="brush: js">
function fetch_current_data() {
  // The <a href="/en-US/docs/Web/API/GlobalFetch/fetch">fetch</a>() API returns a Promise.  This function
  // exposes a similar API, except the fulfillment
  // value of this function's Promise has had more
  // work done on it.
  return fetch("current-data.json").then((response) =&gt; {
    if (response.headers.get("content-type") != "application/json") {
      throw new TypeError();
    }
    var j = response.json();
    // maybe do something with j
    return j; // fulfillment value given to user of
              // fetch_current_data().then()
  });
}
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-promise.prototype.then', 'Promise.prototype.then')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition in an ECMA standard.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>32</td>
   <td>{{CompatGeckoDesktop(29.0)}} [1]</td>
   <td>{{CompatNo}}</td>
   <td>19</td>
   <td>7.1</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile(29.0)}} [1]</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
   <td>32</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] Gecko 24 has an experimental implementation of <code>Promise</code>, under the initial name of <code>Future</code>. It got renamed to its final name in Gecko 25, but disabled by default behind the flag <code>dom.promise.enabled</code>. <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=918806">Bug 918806</a> enabled Promises by default in Gecko 29.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Promise")}}</li>
 <li>{{jsxref("Promise.prototype.catch()")}}</li>
</ul>
Revert to this revision