【发布时间】:2017-12-23 06:25:28
【问题描述】:
我有一个对象(myObject)。对于这个对象,我创建了一个返回 Promise 的方法 (objectPromise)
function myObject(){
this.number = 2;
this.objectPromise = function(data) {
return new Promise((resolve, reject) => {
if (data == this.number) {
resolve();
} else {
reject();
}
});
};
};
然后我有这个代码1)
obj = new myObject();
myPromise1
.then(obj.objectPromise)
.then(function(result){
})
.catch(function(err){
});
2)
obj = new myObject();
myPromise1
.then(function(result){
obj.objectPromise(result)
})
.then(function(result){
})
.catch(function(err){
});
我不明白为什么 1) 没有调用我的 Promise
【问题讨论】:
-
这很不清楚;
objectPromise是一个返回承诺的函数——这是你想要的吗?您想在这里实现什么目标? -
您的意思是
then(obj.objectPromise)?无论如何,您的代码到处都是。一方面,this.data不存在。此外,您不会向其他thens 返还任何东西。 -
对不起,我弄错了,是的,它返回了一个承诺,但我不明白的是 objectPromise 不是 Object 方法,我不必像 2) 那样编写代码跨度>
-
实例方法。
-
@Li357 我修复了 this.data 和 obj.objectPromise,抱歉。实例方法是什么意思?
.then(obj.objectPromise)和.then(function(result) { obj.objectPromise(result) })有什么区别
标签: javascript node.js promise es6-promise