Esta página foi traduzida do inglês pela comunidade.Saiba mais e junte-se à comunidade MDN Web Docs.
Promise.resolve()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since julho de 2015.
O métodoPromise.resolve(value) retorna um objetoPromise que é resolvido com o valor passado. Se o valor forthenable (ex: tiver um métodothen), a promise retornada irá "seguir" essethenable, 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.
In this article
Sintaxe
Promise.resolve(value);Promise.resolve(promise);Promise.resolve(thenable);
Parametros
- value
Argumento a ser resolvido pela
Promise. Pode também ser umaPromiseou um thenable a resolver.
Valor retornado
APromise que será resolvida com o valor passado ou com aPromise passada como valor, caso o valor seja um objetoPromise
Descrição
A função estáticaPromise.resolve retorna umaPromise de que será resolvida.
Examples
>Usando o método estáticoPromise.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 outraPromise
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 othen funcionaaqui.
Resolvendo thenables e disparando Errors
// Resolving a thenable objectvar p1 = Promise.resolve({ then: function (onFulfill, onReject) { onFulfill("fulfilled!"); },});console.log(p1 instanceof Promise); // true, object casted to a Promisep1.then( function (v) { console.log(v); // "fulfilled!" }, function (e) { // not called },);// Thenable throws before callback// Promise rejectsvar 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 resolvesvar 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
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-promise.resolve> |