willconant / flow-js

Javascript Library for Multi-step Asynchronous Logic
MIT License
302 stars 15 forks source link

How to support success and failure callbacks from single async call #3

Closed abierbaum closed 8 years ago

abierbaum commented 13 years ago

I really like the way the code for flow-js looks, but I am having trouble wrapping my head around how I could call an async method that takes two callback methods. One for success and one for failure.

Example method:

myRequest: function(successCb, failureCb) { ... }

I would need a flow that chooses the next method to hit in the flow based upon the success or failure. Something like:

flow.exec(
    function() {    // step 1
        myRequest(this.FN1(), this.FN2())
    },function(successParams) {  // step 2
        // do something
    },function(errParams) {  // step 1 error
        if (err) throw err;
    },function(param) {   // step 3
       // do something more
    }
);

So on success the flow would be: step1, step2, step3. On failure it would be: step1, step1 error, throw.

Is this possible?

willconant commented 13 years ago

Sorry for the incredibly delayed response. Currently, flow-js doesn't have any internal support for this sort of thing. You could always write little wrappers for catching success or failure and then moving to the next step of the flow with some sort of status indicator:

flow.exec(
    function() {
        var flow = this;
        function success(params) { flow('success', params) }
        function failure(params) { flow('failure', params) }
        myRequest(success, failure);
    },function(status, params) {
        if (status === 'success') { ... }
    }
)