Javascript nodejs中的多级承诺
我对诺言有异议 我想使用promise获得所有类别和子类别(多级类别)。但我不能得到多层次的承诺。我可以得到第一个承诺使用承诺。全部(数组)但不能得到子承诺Javascript nodejs中的多级承诺,javascript,node.js,promise,Javascript,Node.js,Promise,我对诺言有异议 我想使用promise获得所有类别和子类别(多级类别)。但我不能得到多层次的承诺。我可以得到第一个承诺使用承诺。全部(数组)但不能得到子承诺 getChild: function(_cats){ var arrCat=[]; var that = this; for (var i = 0, len = _cats.length; i < len; i++) { var t = new Promise(function(resolve,re
getChild: function(_cats){
var arrCat=[];
var that = this;
for (var i = 0, len = _cats.length; i < len; i++) {
var t = new Promise(function(resolve,reject){
var a = [];
var _c = _cats[i];
_cats[i].getChildren(function(err, _childs){
a.push({
root: _c,
child: that.getChild(_childs)
});
resolve(a);
})
})
arrCat.push (t);
}
return Promise.all(arrCat);
和响应
[{
root: {value} // it's ok
child: promise(pending) //it's problem
}]
请帮帮我!谢谢大家! 首先提取
cat.getChildren
的预期版本,以明确代码。然后映射到所有类别,并为每个类别返回一个承诺,该承诺仅在通过递归调用getChild
加载每个嵌套子级后才能解析。像这样的东西应该能奏效
函数getChildren(cat){
新承诺(功能(解决){
cat.getChildren(函数(uz,children){
决心(儿童)
})
})
}
getChild:函数getChild(_cats){
回报你的承诺(
_地图(功能(cat){
返回getChildren(cat)
.then(getChild)//加载每个子对象
.then(功能(儿童){
返回{
根:猫,
儿童:儿童
}
})
})
)
}此代码工作正常。多亏了尤里 函数getChildren(cat){ 新承诺(功能(解决){ cat.getChildren(函数(uz,children){ 决心(儿童) }) }) } getChild:函数getChild(_cats){ 回报你的承诺( _地图(功能(cat){ 返回getChildren(cat) .then(getChild)//加载每个子对象 .then(功能(儿童){ 返回{ 根:猫, 儿童:儿童 } }) }) )
}哪一个是孩子的承诺?对不起,英语不好。我想在子数组中得到承诺。谢谢,你可以简单地接受我的答案,而不是复制/粘贴:)你的名字翻了,Children的意思是多个,但实际上它只得到一个,Children是一连串的呼叫。。。。。。。。。。。。。。。。
[{
root: {value} // it's ok
child: promise(pending) //it's problem
}]