简体   繁体   中英

Asynchronous callback flow control

I'm looking to have better flow control for this async waterfall function.

async.waterfall([
  async.apply(osxAppIconName, options.appFile),
  function(iconFileName, callback) {
    var existingIcon = path.join(options.iconDirectory, iconFileName);
    return callback(null, existingIcon);
  },
  async.apply(fs.copy, options.iconFile), //automatically puts in existingIcon
  async.apply(osxAppIconTouch, options.appFile),
], callback);

Right now I'm using async.apply which will inject global function arguments. In this case above I'm good, it takes existingIcon and will add it as the second-to-last argument in fs.copy . Making fs.copy 's arguments options.iconfile , exitingIcon , callback [function] . Which is great!

However, let's say I need exitingIcon later on. Let's say I push this into my waterfall functions array.

async.apply(newFunction, existingIcon)

How would I get existingIcon to this function? Globals? It seems like a headache to manage! I also think that the async.apply function is executing on load, so if I pass it a variable it's gonna use the value of the variable when async.apply executes.

Thoughts? I have a proposal for async.switchboard here . Which attempts to solve this, but it doesn't work.

There's a nifty way around this using async.auto which attaches the result of a function to a prop in the chain.

return async.auto({
  "icon_name": async.apply(osxAppIconName, options.appFile),
  "copy_icon": ["icon_name",
    function(callback, results) {
      results.existing_icon = path.join(options.iconDirectory, results.icon_name);
      return fs.copy(options.iconFile, results.existing_icon, callback);
    }
  ],
  "touch_icon": ["copy_icon", async.apply(osxAppIconTouch, options.appFile)],
}, callback);

For promises check out:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM