diff options
Diffstat (limited to 'files/pt-br/web/javascript/reference/global_objects/promise/then/index.html')
| -rw-r--r-- | files/pt-br/web/javascript/reference/global_objects/promise/then/index.html | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/files/pt-br/web/javascript/reference/global_objects/promise/then/index.html b/files/pt-br/web/javascript/reference/global_objects/promise/then/index.html new file mode 100644 index 0000000000..5e3c015947 --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/promise/then/index.html @@ -0,0 +1,181 @@ +--- +title: Promise.prototype.then() +slug: Web/JavaScript/Reference/Global_Objects/Promise/then +translation_of: Web/JavaScript/Reference/Global_Objects/Promise/then +--- +<div>{{JSRef("Global_Objects", "Promise")}}</div> + +<h2 id="Summary" name="Summary">Resumo</h2> + +<p>O método <strong>then()</strong> retorna uma <strong>Promise</strong>. Possui dois argumentos, ambos são "<strong>call back functions</strong>", sendo uma para o sucesso e outra para o fracasso da promessa.</p> + +<h2 id="Syntax" name="Syntax">Sintaxe</h2> + +<pre class="syntaxbox"><var>p.then(</var><strong style="font-weight: bold;">quandoRealizada</strong><var>, </var><strong style="font-weight: bold;">quandoRejeitada</strong><var>)</var>; + +p.then(function(valor) { + // sucesso + }, function(motivo) { + // rejeitada +}); +</pre> + +<h3 id="Parametros">Parametros</h3> + +<dl> + <dt>quandoRealizada</dt> + <dd>Uma {{jsxref("Function")}} chamada quando a <strong>Promise</strong> é cumprida (Sucesso). Essa função tem um argumento, o valor do cumprimento.</dd> +</dl> + +<dl> + <dt>quandoRejeitada</dt> + <dd>Uma {{jsxref("Function")}} chamada quando a Promise é rejeitada. Essa função tem um argumento, o motivo da recusa.</dd> +</dl> + +<h2 id="Description" name="Description">Descrição</h2> + +<p>Assim como o método .then() e {{jsxref("Promise.prototype.catch()")}} retornam uma <strong>Promise</strong>, eles podem ser encadeados - uma operação chamada <em><strong>composition</strong>.</em></p> + +<h2 id="Exemplos">Exemplos</h2> + +<h3 id="Usando_o_método_then">Usando o método then</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="Encadeando">Encadeando</h3> + +<p>Já que o método then() devolve uma <strong>Promise</strong>, você pode facilmente encadeá-los. </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 +}); + +</pre> + +<p>No exemplo acima, o último <strong>.then()</strong> recebeu a soma value + 1, que resultou em 2, porém se o retorno de value + 1 fosse uma <strong>Promise</strong> que também retornasse value + 1, o resultado seria o mesmo. Note, no exemplo abaixo, que leva 1000ms para a impressão de 2 ocorrer.</p> + +<pre class="brush: js">var p2 = new Promise(function(resolve, reject) { + resolve(1); +}); + +p2.then(function(value) { + console.log(value); // 1 + return new Promise(function (resolve, reject) { + setTimeout(function () { + resolve(value + 1); + }, 1000); + }); +}).then(function(value) { + console.log(value); // 2 +});</pre> + +<p> </p> + +<p> </p> + +<h2 id="sect1"> </h2> + +<h2 id="Especificações">Especificações</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificações</th> + <th scope="col">Status</th> + <th scope="col">Comentários</th> + </tr> + <tr> + <td><a href="https://github.com/domenic/promises-unwrapping">domenic/promises-unwrapping</a></td> + <td>Draft</td> + <td>Standardization work is taking place here.</td> + </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="Compatibilidade_dos_Browsers">Compatibilidade dos Browsers</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(24.0)}} as <code>Future</code><br> + {{CompatGeckoDesktop(25.0)}} as <code>Promise</code> behind a flag[1]<br> + {{CompatGeckoDesktop(29.0)}} by default</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(24.0)}} as <code>Future</code><br> + {{CompatGeckoMobile(25.0)}} as <code>Promise</code> behind a flag[1]<br> + {{CompatGeckoMobile(29.0)}} by default</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>iOS 8</td> + <td>32</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] Gecko 24 possui uma implementação experimental de Promise, 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="Veja_também">Veja também</h2> + +<ul> + <li>{{jsxref("Promise")}}</li> + <li>{{jsxref("Promise.prototype.catch()")}}</li> +</ul> |
