Skip to content

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

Closed
@benpptung

Description

@benpptung

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
})

Activity

aearly

aearly commented on Jan 5, 2015

@aearly
Collaborator

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

benpptung

benpptung commented on Jan 6, 2015

@benpptung
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

knoid commented on Mar 27, 2015

@knoid

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

aearly

aearly commented on Jun 2, 2015

@aearly
Collaborator

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

benpptung commented on Jun 2, 2015

@benpptung
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.

megawac

megawac commented on Oct 31, 2016

@megawac
Collaborator

Yeah 👍 I'd like this.

added a commit that references this issue on Feb 7, 2017
1717027
added a commit that references this issue on Mar 2, 2017
465cfe8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @benpptung@aearly@knoid@megawac

        Issue actions

          Control flow to try/error if result got? · Issue #687 · caolan/async