我有一些使用承诺的问题(来自回调恐怖)
我创造了一个小提琴,但它不起作用......这就是我理解它有效的方式
1)为每个异步承诺创建 2)在每次异步
之后使用if (Meteor.isServer) {
let progressValue = 0;
Meteor.publish('progress', function() {
const self = this;
let lastProgressValue = 0;
const id = 1;
self.added('progress', id, {
progress: progressValue,
total: 100,
});
while (progressValue < 100) {
self.changed('progress', id, {
progress: progressValue,
total: 100,
});
}
self.ready();
});
...
它没有任何作用......
答案 0 :(得分:1)
您没有在链中传递您的承诺值。 .then()
需要传递值 - 所以它看起来像.then(val => a.getSecond("Jim")) ...
等
我已经更新了你的小提琴。
答案 1 :(得分:1)
这不是你如何链接承诺。你在then
中调用一个函数,它不会神奇地调用你的类'方法来创建它们。试试
a.getFirst("foo")
.then(() => a.getSecond("bar"))
.then(() => a.getLast("baz"))
.then(console.log);
上面说的是,这不是你使用的承诺。
答案 2 :(得分:0)
var a = new Tester("male");
var test = a.getFirst(function(){
return "test1";
})
.then(function(){
return "test2";
})
.then(function(){
return "test3";
})
.then(function(test){
console.log(test);
});
这会产生预期的结果,因为它等待承诺完成