Async
JavaScript and
Promises
Sunday, 25 August 13
Callbacks
console.log("Before	
  readFile");
fs.readFile("file1.txt",	
  function(file1Content)	
  {
	
  	
  console.log("File	
  Contents	
  here");
	
  	
  return	
  "#"	
  +	
  file1Content	
  +	
  "#";
});
console.log("After	
  readFile");
Functions are first class citizens in JavaScript
Functions can takes functions as argument and call
them when they are done
Sunday, 25 August 13
Callbacks
console.log("Before	
  readFile");
fs.readFile("file1.txt",	
  function(file1Content)	
  {
	
  	
  console.log("File	
  Contents	
  here");
	
  	
  return	
  "#"	
  +	
  file1Content	
  +	
  "#";
});
console.log("After	
  readFile");
Functions are first class citizens in JavaScript
Functions can takes functions as argument and call
them when they are done
Sunday, 25 August 13
Is JavaScript really
Async?
All of the JavaScript engines including V8
are single-threaded
But all I/O is evented and asynchronous
It is possible via the EventLoop
Sunday, 25 August 13
An entity that handles and processes external
events and converts them into callback
invocations
Every asynchronous operation adds itself to
the EventLoop
Event Loops
Sunday, 25 August 13
How EventLoop
works?
Sunday, 25 August 13
Event Loop
Sunday, 25 August 13
Making Code
Asynchronous
setImmediate
process.nextTick
setTimeout / setInterval
Sunday, 25 August 13
Promises
A Promise is an object that represents a one-time
event, typically the outcome of an async task like an
AJAX call.
At first, a Promise is in a pending state. Eventually,
it’s either resolved or rejected.
Once a Promise is resolved or rejected, it’ll remain in
that state forever, and its callbacks will never fire
again.
Sunday, 25 August 13
What are
Promises For?
Sunday, 25 August 13
Callback Hell
step1(function	
  (value1)	
  {
	
  	
  step2(value1,	
  function(value2)	
  {
	
  	
  	
  	
  step3(value2,	
  function(value3)	
  {
	
  	
  	
  	
  	
  	
  step4(value3,	
  function(value4)	
  {
	
  	
  	
  	
  	
  	
  	
  	
  //	
  Do	
  something	
  with	
  value4
	
  	
  	
  	
  	
  	
  });
	
  	
  	
  	
  });
	
  	
  });
});
Q.fcall(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.then(function	
  (value4)	
  {
	
  	
  	
  	
  //	
  Do	
  something	
  with	
  value4
})
.done();
Promises can solve the problem of “Callback Hell”
Sunday, 25 August 13
Handling exceptions
Promises also helps you handle exceptions in a cleaner way
var	
  handleError	
  =	
  console.log;
step1(function	
  (value1)	
  {
	
  	
  step2(value1,	
  function(value2)	
  {
	
  	
  	
  	
  step3(value2,	
  function(value3)	
  {
	
  	
  	
  	
  	
  	
  step4(value3,	
  function(value4)	
  {
	
  	
  	
  	
  	
  	
  	
  	
  //	
  Do	
  something	
  with	
  value4
	
  	
  	
  	
  	
  	
  },	
  handleError);
	
  	
  	
  	
  },	
  handleError);
	
  	
  },	
  handleError);
},	
  handleError);
Q.fcall(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.then(function(value4)	
  {
	
  	
  	
  	
  //	
  Do	
  something	
  with	
  value4
})
.fail(function(error)	
  {
	
  	
  	
  	
  //	
  Handle	
  any	
  error	
  from	
  all	
  above	
  
steps
})
.done();
Sunday, 25 August 13
It makes the code Async
Q adds the functions in the chain to the Event loop
When the promised function returns, the promises
are rejected/resolved during the next pass of the
runloop
Sunday, 25 August 13
Thank you
Code snippets used for the demo can be downloaded from
https://gist.github.com/senthilkumarv/6326192
Contact at senthilkumar@thoughtworks.com and kuldeepg@thoughtworks.com
Sunday, 25 August 13

Asynchronous JavaScript and Promises

  • 1.
  • 2.
    Callbacks console.log("Before  readFile"); fs.readFile("file1.txt",  function(file1Content)  {    console.log("File  Contents  here");    return  "#"  +  file1Content  +  "#"; }); console.log("After  readFile"); Functions are first class citizens in JavaScript Functions can takes functions as argument and call them when they are done Sunday, 25 August 13
  • 3.
    Callbacks console.log("Before  readFile"); fs.readFile("file1.txt",  function(file1Content)  {    console.log("File  Contents  here");    return  "#"  +  file1Content  +  "#"; }); console.log("After  readFile"); Functions are first class citizens in JavaScript Functions can takes functions as argument and call them when they are done Sunday, 25 August 13
  • 4.
    Is JavaScript really Async? Allof the JavaScript engines including V8 are single-threaded But all I/O is evented and asynchronous It is possible via the EventLoop Sunday, 25 August 13
  • 5.
    An entity thathandles and processes external events and converts them into callback invocations Every asynchronous operation adds itself to the EventLoop Event Loops Sunday, 25 August 13
  • 6.
  • 7.
  • 8.
  • 9.
    Promises A Promise isan object that represents a one-time event, typically the outcome of an async task like an AJAX call. At first, a Promise is in a pending state. Eventually, it’s either resolved or rejected. Once a Promise is resolved or rejected, it’ll remain in that state forever, and its callbacks will never fire again. Sunday, 25 August 13
  • 10.
  • 11.
    Callback Hell step1(function  (value1)  {    step2(value1,  function(value2)  {        step3(value2,  function(value3)  {            step4(value3,  function(value4)  {                //  Do  something  with  value4            });        });    }); }); Q.fcall(promisedStep1) .then(promisedStep2) .then(promisedStep3) .then(promisedStep4) .then(function  (value4)  {        //  Do  something  with  value4 }) .done(); Promises can solve the problem of “Callback Hell” Sunday, 25 August 13
  • 12.
    Handling exceptions Promises alsohelps you handle exceptions in a cleaner way var  handleError  =  console.log; step1(function  (value1)  {    step2(value1,  function(value2)  {        step3(value2,  function(value3)  {            step4(value3,  function(value4)  {                //  Do  something  with  value4            },  handleError);        },  handleError);    },  handleError); },  handleError); Q.fcall(promisedStep1) .then(promisedStep2) .then(promisedStep3) .then(promisedStep4) .then(function(value4)  {        //  Do  something  with  value4 }) .fail(function(error)  {        //  Handle  any  error  from  all  above   steps }) .done(); Sunday, 25 August 13
  • 13.
    It makes thecode Async Q adds the functions in the chain to the Event loop When the promised function returns, the promises are rejected/resolved during the next pass of the runloop Sunday, 25 August 13
  • 14.
    Thank you Code snippetsused for the demo can be downloaded from https://gist.github.com/senthilkumarv/6326192 Contact at senthilkumar@thoughtworks.com and kuldeepg@thoughtworks.com Sunday, 25 August 13