Esta tradução está incompleta. Ajude atraduzir este artigo.
O método Promise.resolve(value)
retorna um objeto Promise
que é resolvido com o valor passado. Se o valor for thenable (ex: tiver um método "then"
), a promise retornada irá "seguir" esse thenable, adotando seu eventual estado; Do contrário a promise retornada 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.
Valor retornado
A Promise
que será resolvida com o valor passado.
Descrição
A função estática Promise.resolve
retorna uma promessa 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 });
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 });
Specificações
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Promise.resolve' in that specification. |
Standard | Initial definition in an ECMA standard. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Promise.resolve' in that specification. |
Draft |
Browser compatibility
To contribute to this compatibility data, please write a pull request against this file: https://github.com/mdn/browser-compat-data/blob/master/javascript/promise.json.
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Servo |
---|---|---|---|---|---|---|---|
Basic Support | 32.0 | (Yes) | 29.0 | No support | 19 | 7.1 | No support |
Feature | Android | Chrome for Android | Edge Mobile | Firefox for Android | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic Support | 4.4.4 | 32.0 | (Yes) | 29 | No support | (Yes) | 8.0 |