Ново обещание, решителност

Примерен код

0
0

оставете срещу обещания за връщане на js

In simple terms, inside a then handler function:

A) When x is a value (number, string, etc):
	- return x is equivalent to return Promise.resolve(x)
	- throw x is equivalent to return Promise.reject(x)

B) When x is a Promise that is already settled (not 
pending anymore):
	- return x is equivalent to return Promise.resolve(x), 
      if the Promise was already resolved.
	- return x is equivalent to return Promise.reject(x), 
      if the Promise was already rejected.
      
C) When x is a Promise that is pending:
	- return x will return a pending Promise, and it will 
    be evaluated on the subsequent then.
Read more on this topic on the Promise.prototype.then() docs.
0
0

Обещавам.d

var original = Promise.resolve(33);
original.then( val => console.log('origianl', val);
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
0
0

обещание за разрешаване отхвърли

function testFunction(value) {
    return new Promise(function (resoleve, reject) {
        setTimeout(function () {
            let n = value;
            if (n < 100) {
                resoleve("範囲内");
            } else {
                reject("範囲外");
            }
        }, 2000);
    })
}
 
testFunction(10)
    .then(function (value) {
        console.log(value);
        return testFunction(50);
    })
    .then(function (value) {
        console.log(value);
        return testFunction(150);
    })
    .catch(function (error) {
        console.log(`エラーです。${error}`);
    })
0
0

обещания в js

getData()
    .then(data => console.log(data))
    .catch(error => console.log(error));
0
0

обещание за разрешаване отхвърли

function testFunction() {
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve('hello!');
        }, 1000);
    })
}
 
testFunction()
    .then(function (value) {
        console.log(value);
    })
    .catch(function (error) {
        console.log(error);
    })

На други езици

Тази страница на други езици

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Íslensk
..................................................................................................................

Популярно в тази категория

Популярните страници с примери в категория