如何确保以下递归函数已解析或完成,然后执行一些操作?

时间:2019-07-16 06:56:55

标签: javascript sharepoint jquery-deferred deferred deferred-execution

我创建了一个递归函数,它将以递归方式从共享点文档库中获取文件详细信息,但无法知道递归函数是已解决还是已完成<​​/ p>

 //function definition
        getFiles = function getFiles(folderUrl) {
                    var deferred = $.Deferred();
                    try {
                        $pnp.sp.web.getFolderByServerRelativeUrl(folderUrl).expand("Folders, Files").get().then(function (r) {
                            r.Folders.forEach(function (item) {
    //recursive call                           
     getFiles(item.ServerRelativeUrl);
                            });
                            r.Files.forEach(function (item) {
                                console.log(item.ServerRelativeUrl);
                                allfilesrecursive.push(item);
                            });
                            deferred.resolve(allfilesrecursive);
                        }).catch(function (err) {
                            Errorlog(JSON.stringify(err), 'getFiles catch', 'TestFileUpload.js', fileuploaddetails.currentBrowserURL);
                            deferred.reject();
                        });

                    } catch (err) {
                        Errorlog(JSON.stringify(err), 'getFiles', 'TestFileUpload.js', fileuploaddetails.currentBrowserURL);
                        deferred.reject();
                    }
                    return deferred.promise();
                }

0 个答案:

没有答案