1

I have written a retry mechanism that should return a promise:

private connect(): Promise<any> {
      return new Promise((resolve, reject) => {
          if(this.count < 4) {
             console.log("count < 4, count:"+this.count);
             this.count++;
             return this.connect();
            } else {
              resolve("YES");
            }
        });
    }

If I call:

t.connect().then((data:any)=>{ console.log("YES:"+data)});

I would like once count >= 4 and resolve is called to be able to trigger the above "then".

Alex
  • 10,869
  • 28
  • 93
  • 165
  • 1
    *"i would like once once count >= 4 and resolve is called to trigger the above "then""* - try to rephrase this sentence. – dfsq Sep 22 '16 at 12:58
  • Possible duplicate of [Recursive Promise in javascript](http://stackoverflow.com/questions/29020722/recursive-promise-in-javascript) – Alex Sep 22 '16 at 13:05
  • [Here](http://stackoverflow.com/a/39217181/4543207) you can find a detailed promise workflow of several sequentially chained promises with each one trying 5 times before rejecting. Achieved by recursive count passing. – Redu Sep 22 '16 at 13:55

2 Answers2

3

You need to resolve inner promise with the new one, return this.connect() is not enough:

function connect(): Promise<any> {
  return new Promise((resolve, reject) => {
    if (this.count < 4) {
      console.log("count < 4, count:" + this.count);
      this.count++;
      resolve(this.connect());
    } else {
      resolve("YES");
    }
  });
}

Note, how you resolve with new recursive promise with resolve(this.connect());.

Check the demo below.

function connect() {
  return new Promise((resolve, reject) => {
    if (this.count < 4) {
      console.log("count < 4, count:" + this.count);
      this.count++;
      resolve(this.connect());
    } else {
      resolve("YES");
    }
  });
}

const t = {count: 0, connect}

t.connect().then(data => console.log(`YES: ${data}`));
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Why do not you use reject? Is there a specific reason or ambiguity to the question? If there is no reason - I'd love to know how to do it =) – Elad May 07 '18 at 11:15
1

Try passing the count into connect

private connect(count = 0): Promise<any> {
      return new Promise((resolve, reject) => {
          if(count  < 4) {
             console.log("count < 4, count:"+count);
             return this.connect(++count);
            } else {
              resolve("YES");
            }
        });
    }
meltuhamy
  • 3,293
  • 4
  • 23
  • 22
  • Better yet: start with `count = 4` and decrement while `count > 0`, so that the number of retries can be changed – kjaquier Sep 22 '16 at 13:02
  • No need to pass count, OP seems to have `count` property defined as part of `t` object. Anyway, this will not work too. – dfsq Sep 22 '16 at 13:04
  • Ah yes, the promise is not resolved. @dfsq's solution will work but you'll need to make sure you reset `this.count` otherwise it'll get resolved every time (unless this is the behaviour you require). – meltuhamy Sep 22 '16 at 13:08