Adding Work to the Event Queue

As you create Node.js applications and design code, you need to keep in mind the event model described in the previous section. To leverage the scalability and performance of the event model, make sure you break up work into chunks that can be performed as a series of callbacks.

When you have designed your code correctly, you can use the event model to schedule work on the event queue. In Node.js applications, you schedule work on the event queue by passing a callback function using one of these methods:

Image Make a call to one of the blocking I/O library calls, such as writing to a file or connecting to a database.

Image Add an event listener to a built-in event such as an http.request or server.connection.

Image Create your own event emitters and add custom listeners to them.

Image Using the process.nextTick option to schedule work to be picked up on the next cycle of the event loop.

Image Use timers to schedule work to be done after a particular amount of time or at periodic intervals.

The following sections discuss implementing timers, nextTick, and custom events. They will give you an idea of how the event mechanism works. The blocking I/O calls and built-in events are covered in subsequent chapters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.47.230