NODE.JS: PROMISES

Promises are software design patterns which which make asynchronous operations easier to reason about.

ASYNCHRONOUS

Node handles multiple things at once through asynchronous operations. AJAX (asynchronous javascript and xml) is an asynchronous operation. Below I will compare a regular ajax call to an ajax call in node:

//AJAX
$.ajax('http://your-service.com', {
success: function(resp) {
console.log(resp);
},
error: function(err) {
console.log(err);
}
});
//AJAX as used in node
request.get('http://your-service.com')
.send()
.end(function(err, resp) {
if (err) {
console.log(err);
return;
}
console.log(resp);
});

While both examples use callbacks, Node uses a single callback which takes an error (err) as the first argument. If no error exists, error will be null. JQuery uses a separate callback for success and error cases.