Esta traducción está incompleta. Por favor, ayuda a traducir este artículo del inglés.

El método Promise.resolve(value) retorna un objeto Promise que es resuelto con el valor dado. Si el valor es un promise, ese promise es devuelto; si el valor es un thenable (i.e. tiene un "then" method), el valor devuelto le seguirá  thenable, adoptando su estado; de otro modo el promise devuelto estará completado con el valor.

Syntax

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

Parameters

value
Argument to be resolved by this Promise. Can also be a Promise or a thenable to resolve.

Return value

A Promise that is resolved with the given value, or the promise passed as value, if the value was a promise object.

Description

The static Promise.resolve function returns a Promise that is resolved.

Examples

Using the static Promise.resolve method

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

Resolving an array

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

Resolving another Promise

var original = Promise.resolve(33);
var cast = Promise.resolve(original);
cast.then(function(value) {
  console.log('value: ' + value);
});
console.log('original === cast ? ' + (original === cast));

// logs, in order:
// original === cast ? true
// value: 33

The inverted order of the logs is due to the fact that the then handlers are called asynchronously. See how then works here.

Resolving thenables and throwing 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
});

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
La definición de 'Promise.resolve' en esta especificación.
Standard Initial definition in an ECMA standard.
ECMAScript Latest Draft (ECMA-262)
La definición de 'Promise.resolve' en esta especificación.
Draft  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Soporte básico32 Si29 No198
FeatureAndroid webviewChrome para AndroidEdge mobileFirefox para AndroidOpera AndroidiOS SafariSamsung Internet
Soporte básico4.4.432 Si29 Si8 Si

See also

Etiquetas y colaboradores del documento

Colaboradores en esta página: ChristianMarca
Última actualización por: ChristianMarca,