let getParameterNames = require('@captemulation/get-parameter-names');
let Context = require('./context');

let Flow = module.exports = function(context) {
  context = context || new Context();
  let nextResultIdx = -1;

  function wrapPromise(promise, resultName) {
    let then = promise.then.bind(promise);
    promise.then = function(fn, reject) {
      let params = fn ? getParameterNames(fn) : [];
      let fnWrapper = function(result) {
        if (nextResultIdx >= 0) {
          context.results[nextResultIdx++] = result;
          if (params[0]) context.results[params[0]] = result;
        } else {
          ++nextResultIdx;
        }
        if (fn) return fn(result);
      }
      let newPromise = then(fnWrapper, reject);
      return wrapPromise(newPromise);
    }
    return promise;
  }

  return wrapPromise(Promise.resolve());
}

@@ -661,26 +661,206 @@
 fn) 
-return fn(result);
+%7B%0A          let nextResult = fn(result);%0A          if (!(nextResult instanceof Promise)) %7B%0A            context.results%5BnextResultIdx%5D = nextResult;%0A          %7D%0A          return nextResult;%0A        %7D
 %0A   

update flow.js for node v4 consistency