Future.wait()不能没有光纤(在等待Meteor.method中的另一个未来时) [英] Future.wait() can't wait without a fiber (while waiting on another future in Meteor.method)
问题描述
在Meteor
中,我正在编写一个方法,该方法将必须检查特定文件的某个路径的子目录.
首先,我想只列出Meteor
中的子目录,然后我child_process.exec
一个简单的bash脚本,列出自上次执行以来添加的文件.
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.
我在使目录发现异步(Error: Can't wait without a fiber
)时遇到一些问题.我已经编写了一个同步版本,但是同时具有fs.readdir
和fs.stat
而不是它们的同步替代选项可以让我捕获错误.
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.
代码如下:
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();
}
错误Error: Can't wait without a fiber
与future2
有关.
当我注释掉Future.wait(dirs)
时,服务器不再崩溃,但这与我试图解决此问题有关. :/
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. :/
我在方法的另一部分中使用的另一个_.map
函数在期货上运行良好. (另请参阅 https://gist.github.com/possibilities/3443021 我从中找到了灵感)
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)
推荐答案
将回调包装到Meteor.bindEnvironment
中,例如:
Wrap your callback into Meteor.bindEnvironment
, example:
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
做很多事情,其中之一是确保回调在Fibre中运行.
Meteor.bindEnvironment
does a lot of things and one of them is to make sure callback is running in a Fiber.
另一种可能有用的是var funcSync = Meteor._wrapAsync(func)
,它利用期货并允许以同步方式调用函数(但它仍然是异步的).
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).
如果您想了解更多信息,请观看有关突发事件的视频: https://www.eventedmind.com/posts/meteor-dynamic-scoping-with-environment-variables
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
这篇关于Future.wait()不能没有光纤(在等待Meteor.method中的另一个未来时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!