Monday, 14 May 2018

javascript - Why are callbacks more "tightly coupled" than promises?




Can you explain me the following phrase (taken from an answer to 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.




Answer



I don't think promises are more or less coupled than callbacks, just about the same.



Promises however have other benefits:




  • If you expose a callback, you have to document whether it will be called once (like in jQuery.ajax) or more than once (like in Array.map). Promises are called always once.


  • There's no way to call a callback throwing and exception on it, so you have to provide another callback for the error case.


  • Just one callback can be registered, more than one for promises, and you can register them AFTER the event and you will get called anyway.


  • In a typed declaration (Typescript), Promise make easier to read the signature.



  • In the future, you can take advantage of an async / yield syntax.


  • Because they are standard, you can make reusable components like this one:



     disableScreen(promiseGenerator: () => Promise) : Promise
    {
    //create transparent div
    return promiseGenerator.then(val=>
    {
    //remove transparent div
    return val;

    }, error=>{
    //remove transparent div
    throw error;
    });
    }

    disableScreen(()=>$.ajax(....));




More on that: http://www.html5rocks.com/en/tutorials/es6/promises/



EDIT:




  • Another benefit is writing a sequence of N async calls without N levels of indentation.



Also, while I still don't think it's the main point, now I think they are a little bit more loosely coupled for this reasons:





  • They are standard (or at least try): code in C# or Java that uses strings are more lousy coupled than similar code in C++, because the different implementations of strings there, making it more reusable. Having an standard promise, the caller and the implementation are less coupled to each other because they don't have to agree on a (pair) of custom callbacks with custom parameters orders, names, etc... The fact that there are many different flavors on promises doesn't help thought.


  • They promote a more expression-based programming, easier to compose, cache, etc..:



      var cache: { [key: string] : Promise };

    function getData(key: string): Promise {
    return cache[key] || (cache[key] = getFromServer(key));
    }




you can argue that expression based programming is more loosely coupled than imperative/callback based programming, or at least they pursue the same goal: composability.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...