0

I am struggling with promises

having the following snippet:

Promise.resolve()
    .then(a("OK1"))
    .then(a("OK2"))
    .then(a("OK3"))
    .then(a("OK4"))
    .then(a("OK5"))
    .then(function() {
        console.log("FINISH");
    })
    .catch(function(e) {
      
        console.log("ERROR: " + e);
    });


function a(i) {
    return new Promise(function(resolve, reject) {
        
        if(i == "OK4") {
           console.log("THROW");
           throw('Error'); //This does not happen
           reject();
           return;
        }
        if(i == "OK2") {
          reject()
          return;
        }
        console.log(i);
        resolve();
    });
}

i want to be able to reject a promise - then the next .then is executed ("OK3") but i am unable to throw exceptions - i want to trigger the .catch on "OK4"

OK4 - is rejected and not outputted, but OK5 still gets executed - is there a way arround it?

this would be my expected output:

OK1
OK3
ERROR: ....
Helmut Januschka
  • 1,578
  • 3
  • 16
  • 34
  • 4
    You must pass a function to `then`, not a promise. You are executing all your functions without any chaining. – Bergi Sep 01 '16 at 06:14
  • ok and how would i handle a resolve on an async function? function a(x) { setTimeout(function() { // HOW TO RESOLVE FROM HERE? },2000); – Helmut Januschka Sep 01 '16 at 06:16
  • You need to wrap it in a `Promise` constructor call just like you did in your question, so that `resolve` is available via closure. Not sure I understand the question? – Bergi Sep 01 '16 at 06:20
  • well this: https://gist.github.com/hjanuschka/45a86a1c078550e0d8836e3550108cd7 - works as expected. where as without the setTimeout - it doesn't work - why is this ? without setTimeout() -> https://gist.github.com/hjanuschka/35a30aa4da2d9a18e5e53bef476ef46f i just don't get it. – Helmut Januschka Sep 01 '16 at 06:27
  • You're still not passing a function to `then` (in either of the gists), so I doubt anything works. – Bergi Sep 01 '16 at 06:34
  • sorry to steal your time - i appreciate your help https://gist.github.com/hjanuschka/a5dfc7b8bf0054a6e3afa3e332e0ca68 maybe this gist is more showing what i wan't to do. is there a `finally`? or something different than `reject`? or is it state-of-the art - to use `.catch` as a final handler, returning different error objects? ( and later on deciding in .catch if it is a error, or just a "end this chain - aka return" – Helmut Januschka Sep 01 '16 at 06:42
  • [There is no](http://stackoverflow.com/q/27283401/1048572) [finally](http://stackoverflow.com/a/32362233/1048572). Regarding "skip all steps till xy", have a look at [How to properly break out of a promise chain?](http://stackoverflow.com/q/29499582/1048572) - nest parts of your chain in an if-statement. – Bergi Sep 01 '16 at 06:54

1 Answers1

1

As Bergi mentioned, the parameter you are passing on each .then runs the function immediately and return a promise. The promise chain needs a reference to a function it can run at a later date.

If you want to run your function with a parameter, you can wrap it in an anonymous function so the promise chain can run the anonymous function at a later time.

a("OK1")
  .then((res)=>{
    return a("OK2")
  })
  .then((res)=>{
    return a("OK3")
  })
  .then((res)=>{
    return a("OK4")
  })
  .then((res)=>{
    return a("OK5")
  })
  .then(()=>{
    console.log("FINISH");
  })
  .catch(function(e) {
    console.log("ERROR: " + e);
  });


function a (i) {
    return new Promise(function(resolve, reject) {

        if(i == "OK4") {
           console.log("THROW");
           throw new Error(i); //This does not happen
        }
        if(i == "OK2") {
          return reject(new Error(i))
        }
        setTimeout(function () {
          console.log(i);
          resolve(i); 
        }, 100)
    });
}

It is ok to pass a function that will eventually return a promise. The function will be passed the value that the previous promise resolved with.

b(1)
  .then(b)
  .then(b)
  .then(b)
  .then((res) => {
    console.log("FINISH",res);
  })
  .catch(function (e) {
    console.log("ERROR: " + e);
  });

function b (i) {
  return new Promise(function (resolve, reject) {
    console.log(i)
    resolve(i+1)
  })
}
Matt
  • 68,711
  • 7
  • 155
  • 158