Skip to content

Instantly share code, notes, and snippets.

@ndnguru
ndnguru / addTwoNumbersAsync.js
Last active November 2, 2017 06:08
Asynchronous Programming Example in Javascript - Add Two Numbers
var executionTimes = []; // an array to hold the time stamps at various stages, during callbacks
// this function simulates an async function to get the X value
function getXValue(callback) {
var num = ~~(Math.random() * 100); //get a number between 0-100 and coerce to an Integer using '~~'
console.log('X will be: ', num);
var delay = ~~(Math.random() * 5000); // set a random delay btw 0-5 sec
console.log('X Delay (in ms): ' + delay);
//now we set a timeout to invoke to callback function