- Step 1: ask people a question without explanation
- Step 2: give them documentation covering this piece and ask again
- Step 3: ask them whether it generally makes sense for them. Why not?
What this code would log?
$.each(['a', 'b'], (first, second) => console.log(first, second));
Options:
-
'a' \n 'b'
-
'a', 0 \n 'b', 1
-
0, 'a' \n 1, 'b'
-
Step one without explanation: I'd say "2"
-
Step two with documentation: I'd change it to "3", because https://api.jquery.com/jQuery.each/ explains it pretty well
-
Step three: I'd say that it doesn't make much sense for me because I'd rather have it consistent with Array.prototype.each
const emitter = new Emitter();
emitter.each(value => console.log(value));
emitter.next('foo');
emitter.next('bar');
Options:
- 'foo', 'bar'
- Nothing
- Error
const emitter = new Emitter();
emitter.next('foo');
emitter.next('bar');
emitter.each(value => console.log(value));
Options:
- 'foo', 'bar'
- Nothing
- Error
const emitter = new Emitter({
run(me){
me.send('foo')
me.send('bar')
}
});
emitter.each(value => console.log(value));
emitter.run();
Options:
- 'foo'
- 'bar'
- 'foo', 'bar'
- Nothing
const emitter = new Emitter({
run(me){
me.send('foo')
setTimeout(() => me.send('bar'), 100);
}
});
emitter.each(value => console.log(value));
emitter.run();
Options:
- 'foo'
- 'bar'
- 'foo', 'bar'
- Nothing
const emitter = new Emitter();
emitter.each(value => console.log("first", value));
emitter.each(value => console.log("second", value));
emitter.next('foo');
emitter.next('bar');
Options:
- "first, foo" / "second, foo" / "first, bar" / "second, bar"
- "first, foo" / "first, bar" / "second, foo" / "second, bar"
- "first, foo" / "first, bar"
- "second, foo" / "second, bar"
- Error
const emitter = new Emitter();
emitter.each(value => console.log("first", value));
emitter.next('foo');
emitter.each(value => console.log("second", value));
emitter.next('bar');
Options:
- "first, foo" / "second, foo" / "first, bar" / "second, bar"
- "first, foo" / "first, bar" / "second, bar"
- Error
const emitter = new Emitter();
emitter.each(object => {
console.log(object.value);
object.value = 'baz';
});
emitter.each(object => console.log(object.value));
emitter.next({ value: 'foo'});
Options:
- "foo", "baz"
- "foo", "bar"
- Error
run(['foo', 'bar'], value => console.log(value));
Options:
- "foo", "bar"
- Nothing
- Error
run(['foo', 'bar']).each(value => console.log(value))
Options:
- "foo", "bar"
- Nothing
- Error
const emitter = new Emitter();
emitter.each(
map(value => value * 2),
value => console.log(value)
);
emitter.next(1);
emitter.next(2);
Options:
- 1, 2
- 2, 4
- 2
- 4
- Error
const emitter = new Emitter();
emitter.each(
map(async (value) => {
const temp = await new Promise((resolve) => {
setTimeout(() => resolve(value * 10), 1000);
});
return temp + 1;
}),
value => console.log(value)
);
emitter.next(1);
emitter.next(2);
Options:
- Promise, Promise
- 11, 21
- NaN, Nan
- Error
const emitter = new Emitter();
emitter.each(
map((value) => {
return new Promise(resolve => resolve(value))
}),
value => console.log(value)
);
emitter.next(1);
emitter.next(2);
Options:
- Promise, Promise
- 1, 2
- Error
const emitter = new Emitter();
emitter
.map(value => value * 2)
.each(value => console.log(value))
emitter.next(1);
emitter.next(2);
Options:
- 1, 2
- 2, 4
- 2
- 4
- Error
const emitter = new Emitter();
emitter.each(
filter(value => value > 2),
map(value => value * 2),
value => console.log(value)
);
emitter.next(1);
emitter.next(2);
emitter.next(3);
emitter.next(4);
Options:
- 6, 8
- Error
run(
[1, 2, 3, 4],
filter(value => value > 2),
map(value => value * 2),
value => same(value, expected.shift())
);
Options:
- 6, 8
- Error
run(
[1, 2, 3, 4]
).filter(value => value > 2)
.map(value => value * 2)
.each(value => console.log(value));
Options:
- 6, 8
- Error
run(
[1, 2, 3, 4, 5, 6],
limit(2),
map(async (value) => {
return await new Promise(resolve => setTimeout(() => resolve(value * 10), 500));
}),
value => console.log(value)
);
Options:
- 1, 2
- 10, 20
- 10, 20, 30, 40, 50, 60
- Error