1

I am writting a react + typescript application with Visual Studio Code:

enter image description here

react.js: 16.2.0 typescript: 2.6.2

I am using the built-in Promise library of vscode. But somehow I cannot propagate rejected promise to the caller function. Here I would like to simplify as much as I can my code (it is an already complex application).

Say I have an inner function Foo that returns a promise:

const Foo = (args) => {

    return new Promise( (resolve, reject) => {

         // 1. call a remote backend method (e.g ajax)
         // 2. on call success:
               resolve(success);

         // 3. on call fails:
               reject(error); // --> this error does not propagate
    }); 
}

Now here is the outer function Bar that calls Foo:

const Bar = (args) => {

    new Promise( (resolve, reject) => {

         Foo(args).then( success => {

            //...success code

         }).catch(error => {
            // ! this handle is never called
         }); 
    }) 
}

On the outer function Bar, the catch handler is never called!! It is like the inner promise rejection fails silently. I swear this was working until yesterday...I have spent half my Sunday trying to figure out was is going on.

I don't know anymore what to do. So I turn to Stackoverflow and the community. Is there anything I am missing about using Promise with Typescript? (before I was using the Q.js library. But I switched to the embedded promise library which comes with Vscode). Any help will really help me.

TheSoul
  • 4,906
  • 13
  • 44
  • 74
  • Have you tried to put a `console.log()` on the `// 3. on call fails:` block to see if the `reject` function is actually being called? – Carlo Feb 04 '18 at 23:15
  • The reject function is actually called. I have set a breakpoint on this line. And it actually stops there. – TheSoul Feb 04 '18 at 23:16
  • One thing I have omitted (as I said, I am trying to summarize my sample code) is that the inner function Foo uses the q.js promise libray. I think this messes up with the built-in promise library of vscode – TheSoul Feb 04 '18 at 23:18
  • Have you tried to change the promise library? – Carlo Feb 04 '18 at 23:20
  • Unfortunately I cannot change the promise library. The inner foo is a third-party libray that uses the q.js And my application uses the built-in promise lib of vscode. This was working until recently. Few days ago, I upgraded the vscode to the 1.19.3 version. I wander if this upgrade has broken my code – TheSoul Feb 04 '18 at 23:24
  • If you are using typescript, and the function `Foo` returns a promise, why not use `async await` in the function `Bar`. – Akash Dathan Feb 05 '18 at 12:24
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Feb 05 '18 at 13:34

2 Answers2

1

On the outer function Bar, the catch handler is never called!! It is like the inner promise rejection fails silently. I swear this was working until yesterday...I have spent half my Sunday trying to figure out was is going on.

If the reject is called after resolve has been called then the promise does not reject as it is settled on fulfilled. Hence catch handlers will not be called.

basarat
  • 261,912
  • 58
  • 460
  • 511
  • I think that with the comments he meant some sort of "if" statements being used, I don't think this is the problem – Carlo Feb 04 '18 at 23:12
  • Thanks for the answer. But the resolve is not called because the remote call fails. So only the reject is called – TheSoul Feb 04 '18 at 23:12
0

An optional second function can be passed to .then() to handle rejection.

const Bar = (args) => {

    new Promise( (resolve, reject) => {

         Foo(args).then( success => {

            //...success code

         }, error => {
            //...handle error
         }) 
    }) 
}
Akash Dathan
  • 4,348
  • 2
  • 24
  • 45