0

I am experimenting with Promise from ES6, but I can't find any alternative to complete as in jQuery ajax. I need to execute function after all the registered handlers with "then".

Thanks!

stefo0O0o
  • 119
  • 1
  • 11
  • Try `.then(fn, fn)`. –  Dec 21 '15 at 17:00
  • 1
    What exactly do you mean by "*after all the registered handlers*"? What do you need this for? – Bergi Dec 21 '15 at 18:04
  • I have 2 modules A and B. Module A creates the Promise and returns it to module B. In module A, before I return the Promise I want to attach handler that I want to be executed always as the last, since I expect something to happen in one of the handlers attached in module B when module B gets the Promise instance. I need this handler attached in module A before the Promise is returned to be executed last no matter how many handlers you attach in module B. – stefo0O0o Dec 21 '15 at 20:02
  • @stefo0O0o: That's not possible with a plain promise. For something like this, you should use the [promise disposer pattern](http://stackoverflow.com/q/28915677/1048572) – Bergi Dec 22 '15 at 00:53

1 Answers1

2

As mentioned by Bergi, what you want is the disposer pattern. Your central conception of a promise appears to be a bit off, and I think that is making this harder for you to reason about. When you call .then, you are not conceptually "attaching a handler", you are creating a new promise that will by definition resolve after all of its .then handlers have run.

Given your central issue based on code like this:

// a.js
module.exports = function(){
    // Where 'Promise.resolve()' is a stand in for your ajax.
    return Promise.resolve()
        .then(function(){
            // Want this to run after 'B'.
        });
}

// b.js
var makePromise = require('./a');
module.exports = function specialMakePromise(){
    return makePromise().then(function(){
        // Should run first.
    });
}

They will always run in the wrong order, because by definition, the .then handler from a.js must run and complete before the .then handler from b.js.

One way to approach this problem would instead to structure your code like this:

// a.js
module.exports = function(callback){
    return Promise.resolve()
        .then(callback)
        .then(function(){
            // Want this to run after 'B'.
        });
}

// b.js
var makePromise = require('./a');
module.exports = function specialMakePromise(){
    return makePromise(function(){
        // Should run first.
    });
}
Community
  • 1
  • 1
loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • Yes, looks exactly like the disposer pattern :-) – Bergi Dec 22 '15 at 12:15
  • 1
    Yup, didn't know the name of it! – loganfsmyth Dec 22 '15 at 18:04
  • Thanks for the answers guys. I see now, but was thinking if this is possible with ES6 native. The restructuring seem fine, but my idea was that the user of this promise is not aware about what I inject before/after the attached handlers with then. Problem resolved! – stefo0O0o Dec 23 '15 at 11:37