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

Helper method for node functions #25

Closed
guybedford opened this issue May 11, 2014 · 4 comments
Closed

Helper method for node functions #25

guybedford opened this issue May 11, 2014 · 4 comments

Comments

@guybedford
Copy link

I find myself defining this in every single source file when I'm using promises in NodeJS:

function asp(fn) {
  return function() {
    var self = this;
    var args = Array.prototype.splice.call(arguments, 0);
    return new Promise(function(resolve, reject) {
      args.push(function(err, val) {
        if (err)
          return reject(err);
        resolve(val);
      });
      fn.apply(self, args);
    });    
  }
}

So you can do things like:

  asp(fs.readFile)('some/file').then(function(content) {
  });

Does this seem sensible? Is there a helper that already does this? Can we make a helper that does so this solution doesn't need to be repeated?

@stefanpenner
Copy link
Owner

The parent library that this one comes from is RSVP and it has a helper method called denodify. This library will not include it as it is the bare bones es6 promises

@guybedford
Copy link
Author

Amazing, thanks - I'll use that! Is there documentation on this somewhere?

@stefanpenner
Copy link
Owner

@guybedford
Copy link
Author

Thanks!

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

No branches or pull requests

2 participants