From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../global_objects/promise/resolve/index.html | 144 +++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 files/pt-br/web/javascript/reference/global_objects/promise/resolve/index.html (limited to 'files/pt-br/web/javascript/reference/global_objects/promise/resolve/index.html') diff --git a/files/pt-br/web/javascript/reference/global_objects/promise/resolve/index.html b/files/pt-br/web/javascript/reference/global_objects/promise/resolve/index.html new file mode 100644 index 0000000000..f4c1220158 --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/promise/resolve/index.html @@ -0,0 +1,144 @@ +--- +title: Promise.resolve() +slug: Web/JavaScript/Reference/Global_Objects/Promise/resolve +tags: + - ECMAScript6 + - ES6 + - JavaScript + - Método(2) + - Promise +translation_of: Web/JavaScript/Reference/Global_Objects/Promise/resolve +--- +
{{JSRef}}
+ +

O método Promise.resolve(value) retorna um objeto {{jsxref("Promise")}} que é resolvido com o valor passado. Se o valor for thenable (ex: tiver um método {{jsxref("Promise.then", "\"then\"")}}), a promise retornada irá "seguir" esse thenable, adotando seu estado final; se o valor for uma promise, o objeto será o resultado da chamada Promise.resolve; do contrário a promise será realizada com o valor.

+ +

Sintaxe

+ +
Promise.resolve(value);
+Promise.resolve(promise);
+Promise.resolve(thenable);
+
+ +

Parametros

+ +
+
value
+
Argumento a ser resolvido pela Promise. Pode também ser uma Promise ou um thenable a resolver.
+
+ +

Valor retornado

+ +

A {{jsxref("Promise")}} que será resolvida com o valor passado ou com a {{jsxref("Promise")}} passada como valor, caso o valor seja um objeto {{jsxref("Promise")}} 

+ +

Descrição

+ +

A função estática Promise.resolve retorna uma Promise de que será resolvida.

+ +

Examples

+ +

Usando o método estático Promise.resolve

+ +
Promise.resolve("Success").then(function(value) {
+  console.log(value); // "Success"
+}, function(value) {
+  // not called
+});
+
+ +

Resolvendo um array

+ +
var p = Promise.resolve([1,2,3]);
+p.then(function(v) {
+  console.log(v[0]); // 1
+});
+
+ +

Resolvendo outra Promise

+ +
var original = Promise.resolve(true);
+var cast = Promise.resolve(original);
+cast.then(function(v) {
+  console.log(v); // true
+});
+
+
+ +

A ordem invertida dos logs acontece devido ao fato de que os handlers são chamados assincronamente. Veja como o then funciona aqui.

+ +

Resolvendo thenables e disparando Errors

+ +
// Resolving a thenable object
+var p1 = Promise.resolve({
+  then: function(onFulfill, onReject) { onFulfill("fulfilled!"); }
+});
+console.log(p1 instanceof Promise) // true, object casted to a Promise
+
+p1.then(function(v) {
+    console.log(v); // "fulfilled!"
+  }, function(e) {
+    // not called
+});
+
+// Thenable throws before callback
+// Promise rejects
+var thenable = { then: function(resolve) {
+  throw new TypeError("Throwing");
+  resolve("Resolving");
+}};
+
+var p2 = Promise.resolve(thenable);
+p2.then(function(v) {
+  // not called
+}, function(e) {
+  console.log(e); // TypeError: Throwing
+});
+
+// Thenable throws after callback
+// Promise resolves
+var thenable = { then: function(resolve) {
+  resolve("Resolving");
+  throw new TypeError("Throwing");
+}};
+
+var p3 = Promise.resolve(thenable);
+p3.then(function(v) {
+  console.log(v); // "Resolving"
+}, function(e) {
+  // not called
+});
+
+ +

Especificações

+ + + + + + + + + + + + + + + + + + + +
EspecificaçãoStatusComentário
{{SpecName('ES6', '#sec-promise.resolve', 'Promise.resolve')}}{{Spec2('ES2015')}}Definição inicial no padrão ECMA.
{{SpecName('ESDraft', '#sec-promise.resolve', 'Promise.resolve')}}{{Spec2('ESDraft')}}
+ +

Compatibilidade dos navegadores

+ + + +

{{Compat("javascript.builtins.Promise.resolve")}}

+ +

Veja também

+ + -- cgit v1.2.3-54-g00ecf