Skip to content

Mapping over Objects - #1177

Merged
megawac merged 5 commits into
masterfrom
map-objects
Jun 5, 2016
Merged

megawac merged 5 commits into
masterfrom
map-objects

Conversation

@aearly

@aearly aearly commented May 31, 2016

Copy link
Copy Markdown
Collaborator

WIP. Closes #1157.

I might add mapKeys and family later, but I can't contrive an example use-case where you'd need to map the keys of an object asynchronously.

@aearly aearly added this to the 2.0 milestone May 31, 2016
Comment thread mocha_test/mapValues.js
async.mapValuesLimit(obj, 2, function (val, key, next) {
running++;
async.setImmediate(function () {
expect(running).to.equal(concurrency[key]);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I'm experimenting with this way of verifying concurrency. It should be more reliable than asserting a concurrency after some timeout value. Could help with some of our flaky tests.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, let me know if you want testingbot keys to try running in selenium

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd have to refactor other tests to be like this style, tracking concurrency and verifying things finish in a certain order, but it might be worth it 😓

Comment thread lib/internal/map.js Outdated
eachfn(arr, function (value, index, callback) {
iteratee(value, function (err, v) {
results[index] = v;
var idx = keys ? indexOf(keys, index, 0) : index;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't be necessary. Just keep track of by creating and incrementing a numeric index variable (idx in the eachfn

var idx = 0;
eachfn(arr, function(...) {
   idx += 1;
   iteratee(...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Fixed in 611a442

Comment thread mocha_test/mapValues.js
var assert = require('assert');

describe('mapValues', function () {
var obj = {a: 1, b: 2, c: 3};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth testing with an array as well?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would the expected behavior be?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{0: <val>, 1: <val>, 2: <val>}

I would also be fine with leaving this behaviour implied

@megawac

megawac commented Jun 3, 2016

Copy link
Copy Markdown
Collaborator

LGTM /cc me when you feel done with this PR and I'll review again+merge

@aearly

aearly commented Jun 5, 2016

Copy link
Copy Markdown
Collaborator Author

It's ready to merge as-is. I could add mapKeys, but as I said, I'm struggling coming up with a real-world-ish example where you'd want to iterate and update the keys of an object asynchronously.

@megawac
megawac merged commit 872cb91 into master Jun 5, 2016
@aearly
aearly deleted the map-objects branch June 5, 2016 23:50
hargasinski pushed a commit to hargasinski/async that referenced this pull request Jun 29, 2016
@RobertWHurst

RobertWHurst commented Oct 28, 2016 •

Copy link
Copy Markdown

I thanks think of plenty. For one, an object containing urls.
I'm not sure why you decided to remove this.

async.map({
  resource1: 'http://resource1.url',
  resource2: 'http://resource1.url'
}, (url, cb) => fetchResource(url, cb), (err, results) => { ... });

Update: If anyone is curious about how to continue mapping objects, there is mapValues instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants