我明白,我必须回复承诺,而不是承诺通过承诺的结果。
但是我似乎无法实现这一点,比如我有一个成员方法,如:
CreateNextBatch() {
this.orders.clear();
let maxNum = this.maxNum;
let counter = this.orderCounter;
let CreateThem = (counter, r) => {
if (r >= 0) {
//Order.find() finds an entry in a database
Order
.find({orderNr: counter.fullNumber()})
.then(function(orders) {
console.log("created order " + counter.fullNumber().toString());
let num = r;
if (orders.length === 0) {
this.OpenOrder(counter.fullNumber());
//adds order to this.orders
num -= 1;
}
counter.nextNumber();
return CreateThem(counter, num);
}.bind(this))
.catch (function (err){
console.log(err);
return false;
});
} else {
return true;
}
};
return () => {CreateThem(counter, maxNum);};
}
基本上它以递归方式创建订单,在找到this.MaxRequests
个空白点后终止,然后将它们放在this.orders
下的列表中
现在我通过以下方式调用此函数:
initialLoader.CreateNextBatch().then(function (success) {
console.log(success);
console.log("loaded");
initialLoader.initializeBatch();
});
然而这失败了:
TypeError: initialLoader.CreateNextBatch(...).then is not a function
at LoadExternDatabase...
为什么这不起作用?我不明白的是什么?
编辑:我也尝试用新承诺替换回报:
return new Promise((resolve, reject) => {
CreateThem(counter, maxRequests);
resolve();
});
然而,这会立即执行解析,而不是等待CreateThem完成。也不是return CreateThem.bind(this, counter, maxRequests);
答案 0 :(得分:0)
return new Promise((resolve, reject) => {
globalVarKeep = resolve;
globalVarBreak = reject;
});
在此之前你会有一些代码等待承诺或事件说: -
CreateThem(counter, maxRequests);
globalVarKeep();
或
CreateThem(counter, maxRequests).then(globalVarKeep);
如果那是合适的。
答案 1 :(得分:0)
函数initialLoader.CreateNextBatch()
返回一个函数,而不是一个promise,你应该这样做:initialLoader.CreateNextBatch()()
如果CreateThem
> = 0,则函数this.maxNum
可以返回true,并且取决于递归,承诺为true或false。但并不总是保证会回报承诺。并且() => {CreateThem(counter, maxNum);};
也没有返回任何内容。您可以尝试以下方法:
CreateNextBatch() {
this.orders.clear();
let maxNum = this.maxNum;
let counter = this.orderCounter;
let CreateThem = (counter, r) => {
if (r >= 0) {
//Order.find() finds an entry in a database
//you can return a promise here
return Order
.find({orderNr: counter.fullNumber()})
.then(function(orders) {
console.log("created order " + counter.fullNumber().toString());
let num = r;
if (orders.length === 0) {
this.OpenOrder(counter.fullNumber());
//adds order to this.orders
num -= 1;
}
counter.nextNumber();
return CreateThem(counter, num);
}.bind(this))
.catch (function (err){
console.log(err);
return false;
});
} else {
return Promise.resolve(true);
}
};
//you are not returning CreateThem here
return () => {return CreateThem(counter, maxNum);};
}
答案 2 :(得分:0)
完成异步操作后,你应该resolve
一个承诺。
例如:
function test() {
return new Promise((resolve, reject) => {
setTimeout(() => {
// Resolved here!
// Because asynchronous function `setTimeout` ends here
resolve();
}, 1000);
})
}
在您的代码中,如果r >= 0
,它会再次在数据库中找到一个条目,直到r
小于0
,因此它会在else
块结束:
if (r >= 0) {
// ...
} else {
// ends here
}
也许您可以将代码更改为:
CreateNextBatch() {
return new Promise((resolve, reject) => {
this.orders.clear();
let maxNum = this.maxNum;
let counter = this.orderCounter;
let CreateThem = (counter, r) => {
if (r >= 0) {
//Order.find() finds an entry in a database
Order
.find({orderNr: counter.fullNumber()})
.then(function(orders) {
console.log("created order " + counter.fullNumber().toString());
let num = r;
if (orders.length === 0) {
this.OpenOrder(counter.fullNumber());
//adds order to this.orders
num -= 1;
}
counter.nextNumber();
return CreateThem(counter, num);
}.bind(this))
.catch (function (err){
console.log(err);
reject(err);
});
} else {
resolve(true);
}
};
});
// return () => {CreateThem(counter, maxNum);};
}