问题描述
要访问对象的方法,我们使用点运算符,如 nameOfObject.nameOfMethod()
.这就是我对点运算符的理解.
To access a method of the object we use dot operator like nameOfObject.nameOfMethod()
. This is how I understand the dot operator.
对点运算符的这种理解并不能帮助我理解 JavaScript 中 promise 的语法.例如看下面的代码:
This understanding of the dot operator is not helping me understand the syntax of promises in JavaScript. E.g see the code below:
var askMom = function () {
willIGetNewPhone // calling the promise
.then(function (fulfilled1) {
// yay, you got a new phone
console.log(fulfilled);
})
.then(function (fulfilled2) {
// yay, you got a new phone
console.log(fulfilled2);
})
.catch(function (error) {
// ops, mom don't buy it
console.log(error.message);
});
}
在我看来好像代码在说 - nameOfObject.thenMehtod().thenMethod().catchMethod();
It appears to me as if the code is saying - nameOfObject.thenMehtod().thenMethod().catchMethod();
我怎么理解这个?这是否意味着在 JavaSript 中使用 nameOfObject.method1().method2().method3;
How do I understand this? Does it mean it's normal in JavaSript to call methods of an object by using nameOfObject.method1().method2().method3;
推荐答案
当您调用 .then()
时,您就像在执行任何其他函数一样.然后它返回一个承诺,它也可以调用 .then()
或 .catch()
.
When you call .then()
, you're executing a function just like any other. It then returns a promise that also can have .then()
or .catch()
called on it.
这通常称为链接".
这篇关于javascript 中 Promise 的语法令人困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!