Repository navigation
Mapping over Objects - #1177
Mapping over Objects#1177
Conversation
| async.mapValuesLimit(obj, 2, function (val, key, next) { | ||
| running++; | ||
| async.setImmediate(function () { | ||
| expect(running).to.equal(concurrency[key]); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Cool, let me know if you want testingbot keys to try running in selenium
There was a problem hiding this comment.
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 😓
| eachfn(arr, function (value, index, callback) { | ||
| iteratee(value, function (err, v) { | ||
| results[index] = v; | ||
| var idx = keys ? indexOf(keys, index, 0) : index; |
There was a problem hiding this comment.
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(...| var assert = require('assert'); | ||
|
|
||
| describe('mapValues', function () { | ||
| var obj = {a: 1, b: 2, c: 3}; |
There was a problem hiding this comment.
Worth testing with an array as well?
There was a problem hiding this comment.
What would the expected behavior be?
There was a problem hiding this comment.
{0: <val>, 1: <val>, 2: <val>}
I would also be fine with leaving this behaviour implied
|
LGTM /cc me when you feel done with this PR and I'll review again+merge |
|
It's ready to merge as-is. I could add |
Mapping over Objects
|
I thanks think of plenty. For one, an object containing urls. 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. |
WIP. Closes #1157.
I might add
mapKeysand family later, but I can't contrive an example use-case where you'd need to map the keys of an object asynchronously.