Future.wait () cannot wait without fiber (waiting in another future in Meteor.method)

In Meteor , I am writing a method that will have to check specific path subdirectories for new files. At first, I would like to simply list the subdirectories inside Meteor , after which I child_process.exec is a simple bash script that lists the files added since the last execution.

I am having problems finding the directory in async ( Error: Can't wait without a fiber ). I wrote a synchronous version, but instead of fs.readdir and fs.stat instead of my synchronous alternatives, I managed to catch errors.

Here is 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(); } 

Error Error: Can't wait without a fiber is related to future2 . When I comment on Future.wait(dirs) , the server no longer crashes, but that concerns how I tried to solve this problem.: /

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

+7
future meteor
source share
1 answer

Wrap your callback in Meteor.bindEnvironment , for 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 does a lot of things, and one of them is to make sure the callback works in Fiber.

Another useful thing is var funcSync = Meteor._wrapAsync(func) , which uses futures and allows you to use a function in a synchronous style (but it is still asynchronous).

Watch these videos 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

+14
source share

All Articles