Node.js 如何递归调用返回承诺的函数?
我想提取查询node js服务的所有子文件夹和子文档,每次调用该服务时,都会返回一个这样的项目数组。我不知道文件夹树的深度,所以我想递归调用一个函数,该函数最终将返回一个数组,该数组将包含所有子文件夹和子文档,从根文件夹列表开始。每个文件夹都由文件夹id标识 所以我做了一个“recPromise(fId)”,它返回一个承诺。在内部,这个函数递归地调用recFun(folderId)。我开始从根文件夹调用“recPromise(fId)”,所以一旦所有根承诺都解决了,我就可以继续了Node.js 如何递归调用返回承诺的函数?,node.js,loops,recursion,promise,Node.js,Loops,Recursion,Promise,我想提取查询node js服务的所有子文件夹和子文档,每次调用该服务时,都会返回一个这样的项目数组。我不知道文件夹树的深度,所以我想递归调用一个函数,该函数最终将返回一个数组,该数组将包含所有子文件夹和子文档,从根文件夹列表开始。每个文件夹都由文件夹id标识 所以我做了一个“recPromise(fId)”,它返回一个承诺。在内部,这个函数递归地调用recFun(folderId)。我开始从根文件夹调用“recPromise(fId)”,所以一旦所有根承诺都解决了,我就可以继续了 rootFol
rootFolders.map( folderOfRootlevel =>{
var folderContentPromise = recPromise(folderOfRootlevel.id);
folderContentPromises.push(folderContentPromise);
})
$q.all(folderContentPromises)
.then(function(folderContent) {
// Do stuff with results.
}
function recPromise(fId){
return new Promise((resolve, reject) => {
var items = [];
function recFun( folderId) { // asynchronous recursive function
function handleFolderContent( asyncResult) { // process async result and decide what to do
items.push(asyncResult);
//Now I am in a leaf-node, no child-Folders exist so I return
if (asyncResult.data.childFolders.length === 0){
return items;
}
else {
//child_folders exist. So call again recFun() for every child-Folder
for(var item of asyncResult.data.childFolders) {
return recFun(item._id);
}
}
}
// This is the service that returns the array of child-Folders and child-Docs
return NodeJSService.ListFolders(folderId).then(handleFolderContent);
}
resolve(recFun(fId));
})
}
It works almost as expected except the loop inside else, where I call again recFun().
The NodeJSService will return an array of sub-Folders so I wish to call recfun() for every sub-Folder.
Now, I only get the result of the 1st sub-Folder of the loop,
which makes sense since I have a return statement there.
If I remove the return statement and call like this "recFun(item._id);"
then it breaks the $q.all().
最后,我决定删除Promise包装函数并使用async Wait
var items = [];
(async() => {
for(var item of rootFolders) {
await recFun(item.id)
}
// Do stuff with items
// go on form here..
})()
function listFolders(folderId) {
return new Promise( function( resolve, reject) {
resolve(FolderService.ListFolders(folderId));
})
}
async function recFun(folderId) {
var foldersResponse= await listFolders(folderId);
items.push(foldersResponse);
if (foldersResponse.data.childFolders.length === 0){
return items ;
}
else {
for(var item of foldersResponse.data.childFolders) {
await recFun(item._id);
}
}
}
避开这个!仅供参考,对于(asyncResult.data.childFolders的var项)的循环
是没有意义的,因为您在第一次迭代时返回。可能您需要删除返回
@jfriend00如果您是对的,返回第一个循环没有意义。我知道我必须删除那里的return语句,但如果我这样做,我就无法在q.all()中获得所有承诺响应