Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are callbacks more "tightly coupled" than promises?

Can you explain me the following phrase (taken from an answer to Stack Overflow question What are the differences between Deferred, Promise and Future in Javascript?)?

What are the pros of using jQuery promises against using the previous jQuery callbacks?

Rather than directly passing callbacks to functions, something which can lead to tightly coupled interfaces, using promises allows one to separate concerns for code that is synchronous or asynchronous.

like image 688
Revious Avatar asked Jan 15 '14 15:01

Revious


People also ask

Why are callbacks better than promises?

Both callbacks and promises help make our code asynchronous. Making callbacks async can cause issues such as callback hell, so to avoid this we can use promises instead, doing this helps us avoid this pitfall while keeping our code async and neat.

What is the difference between callbacks and promises?

Callbacks are functions passed as arguments into other functions to make sure mandatory variables are available within the callback-function's scope. Promises are placeholder objects for data that's available in the future.

Are callbacks faster than promises?

So from my findings i assure you ES6 promises are faster and recommended than old callbacks.

What is the advantage of promise over callback async and await?

Async/Await is used to work with promises in asynchronous functions. It is basically syntactic sugar for promises. It is just a wrapper to restyle code and make promises easier to read and use. It makes asynchronous code look more like synchronous/procedural code, which is easier to understand.


1 Answers

A promise is an object that represents the result of an asynchronous operation, and because of that you can pass it around, and that gives you more flexibility.

If you use a callback, at the time of the invocation of the asynchronous operation you have to specify how it will be handled, hence the coupling. With promises you can specify how it will be handled later.

Here's an example, imagine you want to load some data via ajax and while doing that you want to display a loading page.

With callbacks:

void loadData = function(){   showLoadingScreen();   $.ajax("http://someurl.com", {     complete: function(data){       hideLoadingScreen();       //do something with the data     }   }); }; 

The callback that handles the data coming back has to call hideLoadingScreen.

With promises you can rewrite the snippet above so that it becomes more readable and you don't have to put the hideLoadingScreen in the complete callback.

With promises

var getData = function(){   showLoadingScreen();   return $.ajax("http://someurl.com").promise().always(hideLoadingScreen); };  var loadData = function(){   var gettingData = getData();   gettingData.done(doSomethingWithTheData); }  var doSomethingWithTheData = function(data){  //do something with data }; 

UPDATE: I've written a blog post that provides extra examples and provides a clear description of what is a promise and how its use can be compared to using callbacks.

like image 156
Rui Avatar answered Sep 21 '22 13:09

Rui