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

New async.sync like async.apply for sync functions. #671

Closed
reggi opened this issue Nov 26, 2014 · 7 comments
Closed

New async.sync like async.apply for sync functions. #671

reggi opened this issue Nov 26, 2014 · 7 comments
Labels

Comments

@reggi
Copy link

reggi commented Nov 26, 2014

I came up with an async.sync call that's like async.apply except for synchronous functions. Checkout the bottom example that uses path.join async.sync(path.join, "/hello", "world", "awesome").

var path = require("path");
var async = require("async");
var _ = require("underscore");

async.sync = function() {
  var args = arguments;
  args = Array.prototype.slice.call(args);
  var fn =  args.shift();
  return function(callback) {
    var exe = fn.apply(null, args);
    return callback(exe);
  }
}

async.waterfall([
  async.sync(path.join, "/hello", "world", "awesome")
], function(err, results) {
  console.log(err);
  console.log(results);
});
@aearly
Copy link
Collaborator

aearly commented Nov 26, 2014

👍 for the idea. I end up writing this function all the time. Needs a try/catch to handle any possible errors, though. And the final name probably needs some bikeshedding.

@reggi
Copy link
Author

reggi commented Nov 26, 2014

Thanks @aearly! I think the general reaction is that the whole point is that we should be only worrying about async functions. I think this is good for flow-control keeping async functions plug-and-play. It really depends on your philosophy, and there may be a bigger divide there.

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

aearly commented May 19, 2015

I've added acomb.asyncify with this functionality to my helper library. It might be worth adding to async, but I'll wait for more opinions.

@aearly
Copy link
Collaborator

aearly commented Jun 22, 2015

And ideas for what to name this?

  • async.asyncify(JSON.parse)
  • async.ify(JSON.parse)
  • async.sync(JSON.parse)
  • async.fromSync(JSON.parse)
  • async.toAsync(JSON.parse)
  • async.wrapSync(JSON.parse)

@reggi
Copy link
Author

reggi commented Jun 23, 2015

@aearly From my example above, I personally like like sync.

@aearly
Copy link
Collaborator

aearly commented Jun 23, 2015

I'm not a fan of async.sync, because it's not really "syncing" anything -- it's shorthand for an adjective, not a verb. It's also too generic for what it's doing.

Whats your second choice? 😄

@reggi
Copy link
Author

reggi commented Jun 23, 2015

@aearly Makes sense.

I like async.wrapSync because that is more inline with what is being done, and it's easy to read the whole way through without your head wanting to explode.

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

No branches or pull requests

2 participants