4

Will it work when i have multiple thenable functions for single promise object? I don't want nesting in this case. I want to perform different operation on the same promise object.

var a = 0,
t = null,
promiseFun = function () {
    return new Ext.Promise(function (resolve, reject) {
        t = setInterval(function () {
                if (a == 1) {
                    resolve();
                    clearInterval(t);
                } else if (a == 2) {
                    reject();
                    clearInterval(t);
                }
            }, 1000);
    })
};

var promiseObj = promiseFun();
//handler one
promiseObj.then(function () {
    console.log('resolved 1')
}, function () {
    console.log('rejected 1')
}).then(function () {
    console.log('resolved Nest')
}, function () {
    console.log('rejected Nest')
});

//handler two- this is valid ?
promiseObj.then(function () {
    console.log('resolved 2')
}, function () {
    console.log('rejected 2')
});

//OUTPUT:

//resolved 1
//resolved 2
//resolved Nest

Or do i need to wrap in Deffered.

Gowtham S
  • 923
  • 14
  • 39
  • `promiseObj.then()` expects a single function with a parameter, this function is called by the Promise as it resolves, with the result being passed along. You seem to be trying to pass custom resolve/reject handlers; that's not how the pattern works. The second function is supposed to go inside `.catch()` –  Aug 08 '19 at 10:30
  • @ChrisG - `.then()` supports up to two functions passed as parameters. The 2nd function if present is a reject handler. Most people use `.catch()` for that instead, but there are some use cases for using the 2nd function to `.then()`. – jfriend00 Aug 08 '19 at 10:34
  • @ChrisG - `promiseObj.then() expects a single function` nope, 0, 1 or 2 arguments are valid (yes 0) ... the second one being "onrejected" ... also, you can put anything as the argument, non-functions are treated as if they were not there ... that's how `.then(null, onrejectedFn)` == `.catch(onrejectedFn)` – Jaromanda X Aug 08 '19 at 10:35
  • @jfriend00 & Jaromanda X, I see, I should actually look things up before "correcting" people :) –  Aug 08 '19 at 10:38
  • I seem to recall `.then(null, onresolved)` is the only way to implement promises in old internet explorer for some reason ... doesn't like `.catch` or something - can't remember, but in my own Promise implementation, `catch` is written as `Promise.prototype.catch = function (onRejected) { return this.then(null, onRejected); }; ` – Jaromanda X Aug 08 '19 at 10:41

1 Answers1

8

Will it work when i have multiple thenable functions for single promise object?

Yes, that works just fine. This is referred to as branching instead of chaining.

If you have this:

let p = somePromiseGeneratingFunction();
p.then(...).then(...);
p.then(...).then(...);

Then, you've just create two separate and independent promise chains that are each started when p resolves. If that's the logic you want, then that's perfectly OK.

The one thing you can guarantee is that the first p.then() handler will get called before the second p.then() handler gets called, but if there are further asynchronous operations in either chain, then they run independently after that and are no longer coordinated with each other. They are separate and independent promise chains that run according to the timing of their own asynchronous operations. That's why it's referred to as "branching". One chain branches into two chains.

See Is there a difference between promise.then.then vs promise.then; promise.then for a bit more discussion on the topic.

jfriend00
  • 683,504
  • 96
  • 985
  • 979