递归承诺? [英] Recursive Promises?

查看:26
本文介绍了递归承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历 HTML 5 文件系统中的所有文件,并在迭代完成后启动一些事件.由于这是异步 + 承诺,我很难理解它应该如何工作.

I would like to iterate over all files located in the HTML 5 file system and have some event get started once the iteration is complete. Since this is async + promises im having a hard time trying to grasp how it should work.

我正在使用 angularJS 并创建了一个服务来封装 html 5 文件系统特定功能.

I am using a angularJS and have created a service to encapsulate html 5 file system specific features.

这是递归函数:

function walkDirectory(path) {

    fileSystem.getFolderContents(path) //this is the services and it returns a promise containing all files in the current folder or directory
        .then(function(entries) {

            for (var i = 0; i < entries.length; i++) {

                if(entries[i].isFile) {
                    console.log("is File: " + entries[i].name);
                    //do something with file here
                } 
                else if (entries[i].isDirectory) {
                    console.log("is Dir: " + entries[i].name);
                    walkDirectory(entries[i].fullPath);
                }
            }
        });
};

理想情况下,我想像这样调用该函数,并让它返回一个承诺,一旦遍历所有文件,该承诺就会执行.

ideally i would like to call the function like so, and have it return a promise which gets executed once all files have been traversed.

walkDirectory("/").then( function() {
  console.log(done);
});

任何提示/想法如何实现?

Any tips/ ideas how this can be achieved?

一个想法是拥有一个承诺数组,并为每个文件/目录的数组添加一个新的承诺.我的尝试:

an idea would be to have an array of promises and add a new promise to the array for every file/directory. My attempt:

function walkDirectory(path) {

    var defer= $q.defer();
    var promises = [defer.promise];

    fileSystem.getFolderContents(path)
        .then(function(entries) {

            for (var i = 0; i < entries.length; i++) {

                if(entries[i].isFile) {
                    console.log("is File: " + entries[i].name);
                    //do something with file here
                    defer.resolve();
                    promises.push(defer.promise);
                } 
                else if (entries[i].isDirectory) {
                    console.log("is Dir: " + entries[i].name);
                    promises.push(walkDirectory(entries[i].fullPath));
                }
            }
        });

    return $q.all(promises);
};

walkDirectory("/").then(function() {
    console.log("done");
});

这似乎不起作用,因为 done 从未显示在控制台中.

This doesn't seem to be working since done is never displayed in the console.

推荐答案

在填充数组之前返回数组.

You're returning the array before you populate it.

相反,您需要在 then() 回调中返回 $q.all(promises),并返回外部承诺:

Instead, you need to return $q.all(promises) within the then() callback, and return the outer promise:

return fileSystem.getFolderContents(path).then(function(entries) {
    return $q.all(entries.map(function(e) {
        if (e.isFile) {
            // Do something
            return null;  // Don't wait for anything
        } else {
            // Do something
            return walkDirectory(e.fullPath);
        }
    }));
});

这篇关于递归承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆