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 | 138 +++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 files/ru/web/javascript/reference/global_objects/promise/resolve/index.html (limited to 'files/ru/web/javascript/reference/global_objects/promise/resolve') diff --git a/files/ru/web/javascript/reference/global_objects/promise/resolve/index.html b/files/ru/web/javascript/reference/global_objects/promise/resolve/index.html new file mode 100644 index 0000000000..13a8ba9ee9 --- /dev/null +++ b/files/ru/web/javascript/reference/global_objects/promise/resolve/index.html @@ -0,0 +1,138 @@ +--- +title: Promise.resolve() +slug: Web/JavaScript/Reference/Global_Objects/Promise/resolve +tags: + - ECMAScript6 + - JavaScript + - Обещание + - метод +translation_of: Web/JavaScript/Reference/Global_Objects/Promise/resolve +--- +
{{JSRef}}
+ +

Метод Promise.resolve(value) возвращает {{jsxref("Promise")}} выполненый с переданным значением. Если переданное значение является thenable - обьект (т.е. имеет метод {{jsxref("Promise.then", "\"then\" method")}}), возвращаемое обещание будет следовать thenable - обьекту, принимая свое состояние; в ином случае возвращаемое обещание будет выполнено с переданным значением.

+ +

Синтаксис

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

Параметры

+ +
+
value
+
Значение с которым будет выполнено обещание. Может также быть обещанием или обьект подобный обещанию (thenable - обьект имеющий метод then).
+
+ +

Возращаемое значение

+ +

Выполненый с переданным значением {{jsxref("Promise")}}.

+ +

Описание

+ +

Метод Promise.resolve возвращает выполненое обещание (Promise).

+ +

Примеры

+ +

Использование метода Promise.resolve

+ +
Promise.resolve("Success").then(function(value) {
+  console.log(value); // "Success"
+}, function(value) {
+  // не будет вызвана
+});
+
+ +

Выполнение с массивом

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

Выполнение с другим обещанием ( Promise)

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

Выполнение с thenable объектом и выбрасывание исключений

+ +
// Выполнение с thenable объектом
+var p1 = Promise.resolve({
+  then: function(onFulfill, onReject) { onFulfill("fulfilled!"); }
+});
+console.log(p1 instanceof Promise) // true
+
+p1.then(function(v) {
+    console.log(v); // "fulfilled!"
+  }, function(e) {
+    // не вызывается
+});
+
+// Thenable объект выбрасывает исключение
+// перед вызовом колбека Promise resolves
+var thenable = { then: function(resolve) {
+  throw new TypeError("Throwing");
+  resolve("Resolving");
+}};
+
+var p2 = Promise.resolve(thenable);
+p2.then(function(v) {
+  // не вызывается
+}, function(e) {
+  console.log(e); // TypeError: Throwing
+});
+
+// Thenable объект выбрасывает исключение
+// после вызова колбека 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) {
+  // не вызывается
+});
+
+ +

Спецификация

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES6', '#sec-promise.resolve', 'Promise.resolve')}}{{Spec2('ES6')}}Initial definition in an ECMA standard.
{{SpecName('ESDraft', '#sec-promise.resolve', 'Promise.resolve')}}{{Spec2('ESDraft')}}
+ +

Совместимость с браузерами

+ +

{{Compat("javascript/promise","Promise.resolve")}}

+ +

Смотри также

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