Using asynchronous functions
Node.js
, for all its awesomeness, has a flaw: callbacks. Being asynchronous and non-blocking has a ton of advantages, and we can't imagine life without it. But, wouldn't it be great if there were a way to avoid the "callback hell" by writing our code in a synchronous style but still reaping the benefits of asynchronous code? As you might have guessed, Meteor has a way of doing just that. This recipe will show you how to write and handle asynchronous functions in a synchronous style using Meteor.wrapAsync()
.
Getting ready
Because brevity breeds clarity, we will keep this recipe as simple as possible.
Open a terminal window, navigate to where you would like your project to reside, and enter the following commands:
$ meteor create wrap-sample $ cd wrap-sample $ mkdir server $ meteor
How to do it…
We are going to simulate a delayed call to an asynchronous method, using the standard JavaScript setTimeout()
function.
- First, let's prep for...