Javascript Async development - Callback, Promises, Observable

From the inception of early 90’s till date Async programming came long way in web. In 2005 , Jesse James Garrett coined the term AJAX in an article titled “Ajax: A New Approach to Web Applications. But due to ‘callback hell’ people started panicked by AJAX and tried to explored new approaches. Next most renounced word that came into limelight is ‘Promise’ which somehow solved the problem of callback hell but still some problems were there like cancellation of request and work with multiple values over the time. Here we will try to understand how Async world changed over the time.

What is Async ?

In computer science, asynchronous I/O, or “Non-sequential I/O” is a form of input/output processing that permits other processing to continue before the transmission has finished  —  wikipedia.

Callback way 

function fullfill(){ 
console.log(“fullfill”);
}
function reject(){
console.log(“reject”);
}
function receiver(fullfill, reject){
console.log(“success”);
if(true){
fullfill();
}else{
reject();
}
}
receiver(fullfill,reject);

Promise Way

var promise = new Promise(function(fullfill, reject){ 
console.log(“success”);
if(true){
fullfill();
}else{
reject();
}
});
promise.then(function(){
console.log(“fullfill”);
},function(){
console.log(“reject”);
});

Observable Way

var observable = Rx.Observable.create(function(observer){ observer.next(1); 
observer.next(2); 
return function () { 
console.log(‘disposed’); 
};
}) 
var subscription = observable.subscribe(function(x){ 
console.log(x);
},function(error){ 
console.log(error);
},function(){
 console.log(‘completed !’)
}) 
subscription.unsubscribe();

hyrglobalsource - Hire a React, Node, CSS, React Native Developer

Leave a Reply

Your email address will not be published. Required fields are marked *