等待()不能没有纤维等待(等待另一个未来的气象。方法)

时间:2021-10-13 15:59:11

In Meteor, I'm writing a method that will have to check a certain path's subdirectories for new files. I first would like to just list the subdirectories within Meteor after which I child_process.exec a simple bash script that lists files added since the last time it executed.

在流星中,我正在编写一种方法,它必须检查某个路径的子目录以获取新文件。我首先想要列出的是流星后的子目录,然后是我的child_process。exec一个简单的bash脚本,该脚本列出自上次执行后添加的文件。

I'm having some issues getting the directory discovery to be async (Error: Can't wait without a fiber). I've written a synchronous version but having both fs.readdir and fs.stat in stead of their synchronous alternatives allows me to catch errors.

我有一些问题让这个目录发现是异步的(错误:不能等待没有光纤)。我已经编写了一个同步版本,但同时拥有两个fs。readdir和fs。stat代替了它们的同步选择,这让我可以捕获错误。

Here's the code:

这是代码:

function listDirs(dir, isDir){

    var future1 = new Future();fs.readdir(dir, function(err, files){

            if (err)
                throw new Meteor.error(500, "Error listing files", err);

            var dirs = _.map(files, function(file){

                var future2 = new Future();
                var resolve2 = future2.resolver();

                fs.stat(dir+file, function(err, stats){

                    if (err)
                        throw new Meteor.error(500, "Error statting files", err);

                    if (stats.isDirectory() == isDir && file.charAt(0) !== '.')
                        resolve2(err, file);

                });

                return future2;

            });

            Future.wait(dirs);

            //var result = _.invoke(dirs, 'get');

            future1['return'](_.compact(dirs));
        });

        return future1.wait();
    }

The error Error: Can't wait without a fiber has to do with future2. When I comment out Future.wait(dirs) the server doesn't crash anymore, but that's about as far as I got in trying to solve this. :/

错误错误:不能在没有纤维的情况下等待未来。当我评论未来时,等待服务器不再崩溃,但这是我在试图解决这个问题时所做的。:/

Another _.map function I use in another part of the method works fine with futures. (see also https://gist.github.com/possibilities/3443021 where I found my inspiration)

另一个_。我在这个方法的另一部分中使用的map函数对未来很好。(参见https://gist. github.com/bilibilities/3443021,我找到了我的灵感)

1 个解决方案

#1


14  

Wrap your callback into Meteor.bindEnvironment, example:

把你的回调包装成流星。bindEnvironment,例子:

fs.readdir(dir, Meteor.bindEnvironment(function (err, res) {
    if (err) future.throw(err);
    future.return(res);
}, function (err) { console.log("couldn't wrap the callback"); });

Meteor.bindEnvironment does a lot of things and one of them is to make sure callback is running in a Fiber.

流星。bindEnvironment做了很多事情,其中之一就是确保回调在一个光纤中运行。

Another thing that could be helpful is var funcSync = Meteor._wrapAsync(func) which utilizes futures and allows use to call a function in synchronous style (but it is still async).

另一个有用的东西是var funcSync = Meteor._wrapAsync(func),它利用了期货,并允许使用同步样式调用函数(但它仍然是异步的)。

Watch these videos on evented mind if you want to know more: https://www.eventedmind.com/posts/meteor-dynamic-scoping-with-environment-variables https://www.eventedmind.com/posts/meteor-what-is-meteor-bindenvironment

如果你想知道更多信息,请观看这些视频,如果你想知道更多的话:https://www.eventedmind.com/posts/meteor- dynamicscoping -environment-变量https://www.eventedmind.com/posts/meteor- what-- bindenvironment。

#1


14  

Wrap your callback into Meteor.bindEnvironment, example:

把你的回调包装成流星。bindEnvironment,例子:

fs.readdir(dir, Meteor.bindEnvironment(function (err, res) {
    if (err) future.throw(err);
    future.return(res);
}, function (err) { console.log("couldn't wrap the callback"); });

Meteor.bindEnvironment does a lot of things and one of them is to make sure callback is running in a Fiber.

流星。bindEnvironment做了很多事情,其中之一就是确保回调在一个光纤中运行。

Another thing that could be helpful is var funcSync = Meteor._wrapAsync(func) which utilizes futures and allows use to call a function in synchronous style (but it is still async).

另一个有用的东西是var funcSync = Meteor._wrapAsync(func),它利用了期货,并允许使用同步样式调用函数(但它仍然是异步的)。

Watch these videos on evented mind if you want to know more: https://www.eventedmind.com/posts/meteor-dynamic-scoping-with-environment-variables https://www.eventedmind.com/posts/meteor-what-is-meteor-bindenvironment

如果你想知道更多信息,请观看这些视频,如果你想知道更多的话:https://www.eventedmind.com/posts/meteor- dynamicscoping -environment-变量https://www.eventedmind.com/posts/meteor- what-- bindenvironment。