1

I'm wondering why BlueBird wants to have actions in the callback of the promise. Like:

var pr = new Promise(function(successCb, errorCb){
  // do what you need
  successCb();
});
pr.then(function(){
  // react to promise resolved
});

I was expected to have a flow similar to this:

var pr = new Promise;

// do what you need
pr.resolve();
pr.then(function(){
  // react to promise resolved
});

I don't get why the pattern made to avoid callbacks wants me to use callbacks. Is this needed for a specific reason?

Varvara Stepanova
  • 3,489
  • 4
  • 22
  • 26
  • 1
    Isn't ```new Promise(...)``` just a wrapper for callback-like functions? Promisify can do all the work for you. – Evers Dec 02 '15 at 14:22
  • 1
    putting the action code in a callback allows it to be performed at a later point and chained. in short, it's a dependency injection pattern to isolate concerns and provide a flexible "API" for long-running or nested sequential operations. that's why the "call needs to come from inside the house", so that the outside world needs only worry that it's done (or not), not how it's getting done. – dandavis Dec 02 '15 at 14:32
  • 1
    you can code your own lib that would flow like you suggest. in fact, i would try doing so to see what the limitations of such a setup entail, which should give you a deeper appreciation of why promises are built like they are. – dandavis Dec 02 '15 at 14:38

2 Answers2

0

Promises are not ment to avoid callbacks, but (among other things) prevent callback hell, and make error catching easier.

    doAsync1(params, function(err, res) {
      if (err) {
         throw new Error(err);
      }
      doAsync2(res1, function(err, res2) {
        if (err) {
           throw new Error(err);
        } 
        doAsync3(res2, function(err) {
           if (err) {
             throw new Error(err);
           }
           // then do stuff
        }
      }
    }

...will get written like so in promisified land :

  doAsync1(params)

  .then(function(res1) {
    return doAsync2(res1);
  })

  .then(function(res2) {
    return doAsync3(res2);
  })

  .then(function(res3) {
    // Do something
  })

  .catch(err) {
     throw new Error(err);
  });

You can find more details here

Edit: So you are right: in a synchronous code, hence with no callbacks, Promises are useless.

Pandaiolo
  • 11,165
  • 5
  • 38
  • 70
  • My question is about different thing. It is about actions needed for the promise to be done. Why it is in the callback? Am I right that I cannot resolve the promise from outside of this callback? – Varvara Stepanova Dec 02 '15 at 14:31
  • 1
    If you don't have a callback, there is no asynchronous operation. Promises are about asynchronous operations, as explained in the page I linked above. – Pandaiolo Dec 02 '15 at 14:34
  • No :-) With defer it is. I already googled that it is considered to be an anti-pattern. But I'm wondering why. – Varvara Stepanova Dec 02 '15 at 14:57
  • No ? :-) Good explanation about defer here http://www.codelord.net/2015/09/24/$q-dot-defer-youre-doing-it-wrong/ *Note one of the valid use cases about defer: promisify a callback* – Pandaiolo Dec 02 '15 at 15:04
0

A callback is any function that is called by another function which takes the first function as a parameter. It is “called back” at some specified point inside that function. While we are talking about the javascript, we are dealing with callbacks all the time.

You just cannot avoid callbacks, they are at the core of node js library. They enable a balanced, non-blocking flow of control across applications, they are the foundation of almost all promise libraries. In your code, function passed to then is a callback function only.

Promises are effectively a different style for achieving the same effect as callbacks. The advantage here is an abstraction that makes working with callbacks extremely pleasant.

You can go through these links and get a better hold of these concepts:

  1. You are missing the whole point of promises

  2. Why move to promises

  3. Inside promise libraries

keshav
  • 734
  • 6
  • 19