我在试图找出为什么这段代码不起作用。我正在循环一个依赖树(deepObject),对于每一个依赖树,我都想运行一个返回承诺的函数。然后,我希望下一组函数在解析所有承诺之后发生,但是promise.all中的控制台日志没有执行。如果您有更好的方法,ES6也很酷,但是我也很想知道为什么这段代码不能工作。
更新以添加.catch (这没有任何效果)
for (let i = 0; i < keys.length; i++) {
promisesArray.push(promiseFunction(deepObject[keys[i]]));
}
Promise.all(promisesArray).then(resolved => {
console.log('dep promises resolved');
console.log(`resolved: ${JSON.stringify(resolved)}`);
}).catch(err => console.error(new Error(err));
// promiseFunction is recursive
promiseFunction(obj) {
return new Promise((resolve, reject) => {
const keys = Object.keys(deepObj);
for (let j = 0; j < keys.length; j++) {
if (Object.keys(deepObj[keys[j]]).length) {
// adding return statement to following line breaks it
// that was already recommended, see threads
promiseFunction(deepObj[keys[j]]);
} else {
const dep = new Package(keys[j]);
console.log(`skan dep: ${keys[j]}`);
dep.fetch().then(() => {
return anotherFunction();
}).then(() => {
return Promise.all([
promise1(),
promise2(),
promise3(),
]).then(resolved => {
if (resolved[0].propertyINeed) {
resolve(true);
}
resolve(false);
});
}).catch(err => {
console.error(new Error(err));
process.exit(1);
});
}
}
});我知道这段对话- has been discussed - on here before
在上面的第二个环节中,已被接受的答案表明:
如果您正在异步填充数组,则应该得到该数组的承诺,并使用.then(Promise.all.bind(诺言))。如果你不知道什么时候停止添加承诺,这是不可能的,因为它们可能永远不会得到解决。
但是,我没有使用异步来填充数组。我需要这个吗?我在这里错过了什么?
更新
由于我现在在.then()和.catch()中都有错误日志记录,所以似乎有什么问题与它内部发生的情况无关。
在promiseFunction(deepObj[keys[j]]);中断递归之前添加返回语句。我从迭代超过173个对象到68个。添加捕获记录,不添加其他结果。上面的代码被更新,以共享更多的递归fn。当我运行它时,它似乎执行了所有的承诺,但我无法知道这一点。我最关心的是: 1. promise.all的允诺数组包含它应该包含的所有对象;2.捕捉递归对象中所有对象的所有承诺被解析的时刻。
另外,为了记录在案,这些返回承诺的函数中的每一个都必然是异步的。为了简化和删除任何不必要的承诺,我已经多次回顾这一切。任务很复杂。有一系列必须执行的步骤,它们是异步的,它们是链接的,因为它们必须按照特定的顺序被解析。
发布于 2018-06-11 09:46:44
如果(Object.keys(obj[keysj]).length) { promiseFunction(obj[keysj]);}promiseFunction{ //和一些所有解析正确的异步地狱
如果Object.keys(obj[keys[j]]).length是true,那么您就不会调用resolve或reject,因此承诺永远不会兑现。
(请注意,递归调用promiseFunction会创建一个新的承诺,它永远不会被放入Promise.all中)。
由于您的一些承诺没有解决,Promise.all也不会。
您可能需要更多类似以下内容的内容:
var promises = [];
processObject(obj);
Promise.all(promises).then(etc etc);
function processObject(object) {
for ( loop; over; object ) {
if (condition) {
processObject(object[something]);
} else {
promises.push(new Promise( (res, rej) => {
// so something async
});
}
}
}发布于 2018-06-11 09:52:47
让我们看看你的promiseFunction..。
for (let j = 0; j < keys.length; j++) {
if (Object.keys(obj[keys[j]]).length) {
//if you get in here, promiseFunction returns undefined, and it never gets resolved
promiseFunction(obj[keys[j]]);
} else {
const dep = new Package(keys[j]);
console.log(`skan dep: ${keys[j]}`);
// and some more async hell that all resolves correctly
//but even if it resolves correctly, you still have the if...
}
}要做到这一点,只需在if语句中返回您的promiseFunction结果:
for (let j = 0; j < keys.length; j++) {
if (Object.keys(obj[keys[j]]).length) {
//if you get in here, you're saying this promise will depend on how this second promiseFunction will resolve
return promiseFunction(obj[keys[j]]);
} else {
//omitted for brevity...
resolve('your value');
}
}现在,当您进入if语句时,您基本上是在根据第二个promiseFunction是否正确地解决问题的承诺--您将决议委托给另一个承诺。
我从来没有听说过有人给它起这个名字,但是你可以把它看作是承诺递归:)
https://stackoverflow.com/questions/50794503
复制相似问题