Tutorial by Examples

Callback functions in JavaScript Callback functions are common in JavaScript. Callback functions are possible in JavaScript because functions are first-class citizens. Synchronous callbacks. Callback functions can be synchronous or asynchronous. Since Asynchronous callback functions may be more c...
Question: What is the output of code below and why? setTimeout(function() { console.log("A"); }, 1000); setTimeout(function() { console.log("B"); }, 0); getDataFromDatabase(function(err, data) { console.log("C"); setTimeout(function() { ...
Try catch Errors must always be handled. If you are using synchronous programming you could use a try catch. But this does not work if you work asynchronous! Example: try { setTimeout(function() { throw new Error("I'm an uncaught error and will stop the server!"); }, 1...
Callback hell (also a pyramid of doom or boomerang effect) arises when you nest too many callback functions inside a callback function. Here is an example to read a file (in ES6). const fs = require('fs'); let filename = `${__dirname}/myfile.txt`; fs.exists(filename, exists => { if (exi...
v6.0.0 Promises are a tool for async programming. In JavaScript promises are known for their then methods. Promises have two main states 'pending' and 'settled'. Once a promise is 'settled' it cannot go back to 'pending'. This means that promises are mostly good for events that only occur once. The...

Page 1 of 1