Transcript
  • Meteor

Meteor Async Wrappers

From the class:  Meteor Fibers and Dynamics

Now that you know how BIND environment works and you've seen some examples of its use with fibers and dynamic variables, there's a couple of helpers that Meteor provides that should make more sense now. So the first helpers are for timing, for timers. And so, in this example from the last video, we're using setTimeout function and then we called Meteor.BIND environment and we were able to get the correct value of the name variable by doing all of that. But we don't actually have to do this. We can just call Meteor.setTimeout and it does the same thing. And the benefit of using this function is we can just pass in the callback function as we normally would, without having to wrap it with Meteor.BIND environment.

So I'll just go ahead and copy this code up here. And this now looks a lot nicer. And if I refresh over on the right, we should see it's still working.

So what Meteor setTimeout does is just like with setTimeout, it takes this function and it puts it back onto the task queue at a specified time, so in this case two seconds. And it does an extra bit of work, which is to automatically call BIND environment to save the current environment variables and to call this callback with a new fiber, copying over the environment variable. So this is a little bit of a nicer way to write what we had shown to work in the previous example.

Now we can do the same thing with setInterval. So if I simply change this to setInterval, now this will call this callback function every two seconds, creating a new fiber for each time that it's called. And over on the right, every two seconds it seems to be working.

The final helper we'll look at is called wrap async, and we've already seen an example of that in a couple of videos ago. But now you should have a better sense of how it works. To remind you, let's pretend that we have a asynchronous function. So I'll just call this do async I/O. And it'll do is take a callback as a parameter.

And we'll pretend it does some concurrent work and then at some later time, it will call this callback. So it'll put the callback back onto the task queue. And the callback will get called with a null error and then some result. So it'll be a document, for example.

Then what we can do is turn this into a synchronous function that uses fibers and automatically binds the environment and everything. And to do that, we'll create a do sync I/O function and set that equal to meteor dot wrap async. So we're going to wrap an asynchronous function and turn it into a synchronous one. And then we can just call that function synchronously.

And when we get a result, we can print out the results. So I'll say name.get. And then I'll JSON stringify it so we actually see it in the console on the right. I'll get rid of this and press Save.

Oops, it looks like I just have a syntax here. Here I accidentally typed function, so I'll delete that. And when I restart, it should work now. Great.

And we see over on the right that we're handling both requests and then printing out the results of those requests here. So wrap async allows us to take this asynchronous function-- asynchronous, again, is anything that puts a function back on the task queue at a later time-- and then turn it into a synchronous one that works with environment variables.

You can find the timer functions we just looked at in the Meteor package under the timers file. Here's setTimeout and setInterval. You can see they both just call the JavaScript versions of setTimeout and setInterval, but as a callback that passes in this wrapped function bind and catch. And you can see that right here using the BIND environment function that we studied previously.

And the wrap async function is implemented in the helpers js file. And you can see how it takes a function as input and then returns a new function, or wrap function, that works with BIND environment and with fibers. So you may have been using these helpers previously but not really understood how they worked or why you needed to use them. But now that you've studied fibers and BIND environment, you should have a better idea of how these things work and why you should use them.