1

I'm using native node.js's Promises and the question here did give me any point. Basically, I have a function like below:

var foo = foo (resolve, reject) {
    return obj
        .doA()
        .doB()
        .then(function (value) {
            // here I choose if I want to resolve or reject.
        })
        .catch(function(err) {
        });
}

var promise = new Promise(foo);

return promise
    .then(function() {
        // I know here what I have to return.
    })
    .catch(function(err){
        // I want to repeat the foo function until it is resolved and not rejected. 
    })

obj is a promised object. I would to like to retry the foo function as long as the promise is fulfilled; if it's rejected, then try again.

I do not know how to structure the chain. Any help? Thanks.

Community
  • 1
  • 1
Bruno Bruzzano
  • 477
  • 7
  • 21
  • 1
    As long as the promise is fulfilled and as long as the promise is rejected are _super_ similar. The only difference is checking `.catch` vs `.then` - you might want to instead ask how to convert a rejected promise to a fulfilled one and vice versa. Definitely a duplicate. – Benjamin Gruenbaum Nov 15 '15 at 11:20
  • The answers in that duplicate seem to be way overcomplicated and ill suited to this problem - IMHO - and fails to address some basic problems with this code, e.g. no need for `new Promise` in this code at all – Jaromanda X Nov 15 '15 at 11:35
  • @BenjaminGruenbaum duplicate of? I did not see any question about looping a promise until it's fulfilled with node.js's native Promises. Please, share a link. Thanks. – Bruno Bruzzano Nov 15 '15 at 13:14
  • 1
    Pfft, the duplicate this is closed as is practically the same thing... But if you must have the exact same scenario: `const retry = fn => _ => fn().catch(retry(fn))`. – Benjamin Gruenbaum Nov 15 '15 at 13:19
  • Avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572)! – Bergi Nov 15 '15 at 17:00
  • @BenjaminGruenbaum - should that be `const retry = fn => fn().catch(_ => retry(fn));` – Jaromanda X Nov 16 '15 at 00:37
  • @JaromandaX both are valid versions - the former "lifts" the function and returns a new function that does the same as the original but also retries the action - the latter executes the function immediately with retry logic. – Benjamin Gruenbaum Nov 16 '15 at 07:32
  • @BenjaminGruenbaum - I got an error using yours - must be something in the way that it's called then! - on "reject", yours produces `TypeError: retry(...).then is not a function` ... if there's no reject, yours works – Jaromanda X Nov 16 '15 at 07:40
  • @JaromandaX http://jsfiddle.net/y2nrjzvc/ – Benjamin Gruenbaum Nov 16 '15 at 07:56
  • @BenjaminGruenbaum - that's so odd - similar test I did fails with your code (maybe my test is flawed ... likely) ...ahhh, the light bulb just switched on :p - another good day because another thing learned – Jaromanda X Nov 16 '15 at 07:58
  • What does "did give me any point" mean? That question/answer wasn't helpful? – hippietrail Jul 06 '16 at 08:47

1 Answers1

1

Try including function in declaration of foo , using recursion

function foo() {

  var n = String(new Date().getTime()).slice(-1);
  // if `n` < 5 reject `n` , else resolve `n`
  return Promise[n < 5 ? "reject" : "resolve"](n)
    .then(function(value) {
      return value
        // here I choose if I want to resolve or reject.
    })
    .catch(function(err) {
      return Promise.reject(["rejected", err])
    });
}

(function repeat() {
  var promise = Promise.resolve(foo());

  return promise
    .then(function(data) {
      console.log("complete", data)
        // I know here what I have to return.
    })
    .catch(function(err) {
        // I want to repeat the foo function until it is resolved and not rejected. 
      console.log(err);
      if (err[0] === "rejected") repeat()
    })
}())
guest271314
  • 1
  • 15
  • 104
  • 177
  • 1
    could you explain better? where resolve and reject handles are? – Bruno Bruzzano Nov 15 '15 at 10:22
  • @BrunoBruzzano _"where resolve and reject handles are?"_ ? At `foo` . If promise is rejected at `foo` , call `repeat` recursively until `foo` returns a resolved promise value – guest271314 Nov 15 '15 at 10:25
  • @BrunoBruzzano See updated post , included stacksnippets to demonstrate approach – guest271314 Nov 15 '15 at 10:46
  • 1
    Thanks @guest271314, I'm gonna try this as soon as possible, even if I have a doubt. All this stuff is within a function which has to return a promised obj - let's call it as "myPromisedObject". It's already there, so no need to create. Where I have to return it? I mean, the immediate function should return something? – Bruno Bruzzano Nov 15 '15 at 11:23
  • thanks @guest271314, I solved with your solution. – Bruno Bruzzano Nov 15 '15 at 17:00