递归的承诺? [英] Recursive Promises?

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

问题描述

我想遍历位于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");
});

这似乎并没有因为做的是从来没有在控制台中显示的是工作。

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

推荐答案

在填充之前你返回数组。

You're returning the array before you populate it.

相反,你需要返回 $ q.all(承诺)则()回调之内,返回外承诺:

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天全站免登陆