Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control flow to try/error if result got? #687

Closed
benpptung opened this issue Dec 22, 2014 · 6 comments
Closed

Control flow to try/error if result got? #687

benpptung opened this issue Dec 22, 2014 · 6 comments

Comments

@benpptung
Copy link

Hi ... I love async!! and use a lot of it, especially, I love compose.
Thank you for the great module!! However, I cannot find a control flow to try/error the callback, is there any control flow function can do the following thing?

That said, I have fn1, fn2, fn3, all of them will callback an error or result, if fn1 get the result, then the control flow end, or continue to fn2, then fn3. For example, it might look like:

async.try( [ 
  function(next){
     do_first_try(param, function(err, res){
         if (err) return next(err);
         next(null, res); /// the control flow end, because we have result now
     }
  }, 
  function(next){
    do_second_try(param, function(err, res){
        if (err) return next(err);
        next(null, res); /// the control flow end, because we have result now
    }
  }, 
  function(next){
    do_third_try(param, function(err, res){
        if (err) return next(err);
        next(null, res); /// the control flow end, because we have result now
    }
  },], function( err, result) {
       /// get the result whatever it comes from first try or third try, or err if no result
})
@aearly
Copy link
Collaborator

aearly commented Jan 5, 2015

Interesting, it's kind of like series, except with the inverse of how it handles errors. It definitely could come in handy.

@benpptung
Copy link
Author

yes :) in fact, I am using series doing this now, done(res) to stop the try/error and return the result or done(null)to continue the next try, but it is looking like an anti-pattern to callback.

@knoid
Copy link

knoid commented Mar 27, 2015

+1
I'll try using series for now. Thanks for the tip.

@aearly aearly added the feature label May 19, 2015
@aearly
Copy link
Collaborator

aearly commented Jun 2, 2015

After further thought -- is this any different from retry? This allows you to have a unique function for every try, but is that actually useful? This is also similar to #568

@benpptung
Copy link
Author

I think retry is doing the same task to get a successful response from task no more than specific times before returning an error. . e.g. ping a server alive or not. I think it is different from the idea of try/error, in which, we can try different ways to get a successful result. e.g. the typical way to get XMLHttpRequest in front-end.

@aearly aearly removed the duplicate label Jun 2, 2015
@megawac
Copy link
Collaborator

megawac commented Oct 31, 2016

Yeah 👍 I'd like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants