Okay, so I’m totally new to Meteor.wrapAsync, so far I could manage to do everything synchronous
Following scenario:
I upload a file to a server, the file is a pdf which needs to be base64 encoded.
So I got this:
convertFile = () ->
require('file-base64').encode(file,(err,b64Str) ->
if err then throw err
opts.data.file = b64Str
HTTP.post(process.env.UPLOAD_URL,opts)
)
convertFileSync = Meteor.wrapAsync(convertFile)
convertFileSync()
The only problem is the HTTP call which throws an “Meteor code must always run within a Fiber” error.
I had the same problem with the encoding function as well, but managed to resolve that by using Meteor.wrapAsync on it (hopefully I used it right at least for this one).
How do I get to make the HTTP call now without getting the error?
And more interesting: How do I get a callback from that HTTP call?
According to the docs, I can do either HTTP.post(URL,opts,callback)
or result = HTTP.post(URL,opts)
Tried both, but I guess it fails before the call is even made…
So assuming I wouldn’t be using the HTTP class but my own crappy Ajax function, I would need to wrap that as well? or since it is running inside a wrapped function it doesn’t need to be wrapped anymore?
In theory you should bind each callback of your crappy function. The key thing is the word callback. Meteor uses fibers, its own way of waiting for a async stuff… and you can’t mix to mechanisms…
I am not sure when to use bindenvironment versus wrapasync, they are a bit the same and use each other under the hood.
But that didn’t change anything other than making the code look uglier and uglier and throwing an error inside the library because it can’t reference itself anymore.
EDIT:
Nevermind, passing the reference to Meteor.wrapAsync did the trick!