1
exports.fakeAccounts = function (Account, callback) {
  async.map([{
    username: 'Jehoon',
    provider: 'farcebook'
  }, {
    username: 'C3P0',
    provider: 'farcebook'
  }], function (opts, cb) {
    Account.upsert(opts, cb);
  }, callback);
};

Works fine, but

exports.fakeAccounts = function (Account, callback) {
  async.map([{
    username: 'Jehoon',
    provider: 'farcebook'
  }, {
    username: 'C3P0',
    provider: 'farcebook'
  }], Account.upsert, callback);
};

throws an error, saying that Account is undefined.

I would have thought that since Account.upsert takes the same arguments it would work. I've solved it for now, but I'm wondering what I'm missing here.

1 Answer 1

1

This doesn't work because the Account.upsert method uses the this value and JavaScript has dynamic this.

You can try binding it explicitly to the this value:

exports.fakeAccounts = function (Account, callback) {
  async.map([{
    username: 'Jehoon',
    provider: 'farcebook'
  }, {
    username: 'C3P0',
    provider: 'farcebook'
  }], Account.upsert.bind(Account), callback);
};
Sign up to request clarification or add additional context in comments.

3 Comments

This issue is also noted in "Common Pitfalls" on their GitHub readme: github.com/caolan/async/blob/master/README.md#common-pitfalls
Oh cool, I didn't know that @Qantas94Heavy . I guess it's a common problem when using async if they have a whole section for it.
Wow, thanks. Sorry for the dumb question. Hopefully it can save future developers from having to read the docs!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.