Docsity

Node.js Asynchronous Programming with Callbacks and Error Handling, Essays (university) of Software Project Management

Partial preview of the text

Download Node.js Asynchronous Programming with Callbacks and Error Handling and more Essays (university) Software Project Management in PDF only on Docsity!

Node Modules: Callbacks and Error

Handling

Jogesh K. Muppala

Two Salient Features of JavaScript

• First-class functions: A function can be treated the

same way as any other variable

• Closures:

– A function defined inside another function has access to all

the variables declared in the outer function (outer scope)

– The inner function will continue to have access to the

variables from the outer scope even after the outer

function has returned

Node, Async I/O and Callbacks

Event Loop (single thread) requests File/ Database/ Processing I/O request Callback I/O request completion

Event Loop

timers I/O callbacks idle, prepare poll check close callbacks incoming: connections, data, etc.

  • timers : this phase executes callbacks scheduled by setTimeout() and setInterval().
  • I/O callbacks : executes almost all callbacks with the exception of close callbacks, the ones scheduled by timers, and setImmediate().
  • idle, prepare : only used internally.
  • poll : retrieve new I/O events; node will block here when appropriate.
  • check : setImmediate() callbacks are invoked here.
  • close callbacks : e.g. socket.on('close', ...).