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
Support waiting for multiple event emissions #15
Conversation
Any reason you went away from using a separate method |
count
and resolveImmediately
optioncount
and resolveImmediately
option
You use
It isn't weird. There's no sense using const data = await pEvent(emitter, event, {resolveImmediately: true});
// 0s later => []
// 1s later => ['a']
// 2s later => ['a', 'b']
// 3s later => ['a', 'b', 'c']
// 4s later => ['a', 'b', 'c', 'd']
const data = await pEvent(emitter, event, {count: 1, resolveImmediately: true});
// 0s later => []
// 1s later => ['a'] You can achieve the same by: const data = await pEvent(emitter, event); // 'a'
const data = pEvent(emitter, event, {count: Infinity, resolveImmediately: true});
const result = await data;
setTimeout(() => data.cancel(), n);
Answer:
How do you imagine storing a few items in one item? :P You have to use an array. |
I can do |
I just think the separation would be good. |
You say that, but is it really obvious when reading the code. If I see one call that has |
Swift has taught me the value of having a single return value. It's much more predictable and readable. |
This does not address my concern. You are not generating |
Having had some time to think about this today, I'm even more convinced now that a separate method is the right call. Let's call it |
Do you mean passing a function?
Will do :) |
Yes: const data = await pEvent(emitter, event, {count: generateRandomCount()}); It's not clear from reading this that data could sometimes be a value and sometimes and array. |
-const data = await pEvent(emitter, event, {count: generateRandomCount()});
+const data = await pEvent(emitter, event, {count: generateRandomCount}); NEVERMIND, I got it now! :D (by |
Correct |
Maybe we should introduce it as an option? pEvent(emitter, 'event', {
multiple: true,
count: 2
}); // returns ['a', 'b'] pEvent(emitter, 'event', {
multiple: false,
count: 2
}); // waits for two events, but returns only the first one Also would get rid of these lines of code: Lines 88 to 96 in 23481b5
|
I still think it should be a separate method. I considered a
I don't see the point of this. If you want to wait for two event, but only use the first event, you just do
I'd rather optimize for the public API than internal reducing code. |
Then the PR's ready to be reviewed :) |
You need to update the first paragraphs in the readme, as they assume support for only a single event. |
readme.md
Outdated
Type: `boolean`<br> | ||
Default: `false` | ||
|
||
States if the promise should resolve immediately. Emitting one of the `rejectionEvents` won't throw an error. Example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should provide a use-case for the option, as it's not immediately clear, and also clarify that the returned array will be mutated. when new events are emitted.
count
and resolveImmediately
optionCo-Authored-By: szmarczak <36894700+szmarczak@users.noreply.github.com>
index.js
Outdated
}, options); | ||
if (isNaN(options.count) || options.count < 1) { | ||
throw new TypeError('`count` option should be a number greater than 1'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be inside the new Promise
or return a Promise.reject()
. Methods that return a promise should always do so, even in the failure case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. You're wrong. The promise executes a task, does not generate it. We need to verify options in the promise generator, we need to verify the options before a task is scheduled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const foo = pThrottle(invaidOptions); // this wouldn't throw
foo (); // this would throw and should NOT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you're right. Sorry, I just got confused (cuz I work on many things at the same time and my mind is mixed up) :p
index.js
Outdated
multiArgs: false | ||
}, options); | ||
if (isNaN(options.count) || options.count < 1) { | ||
throw new TypeError('`count` option should be a number greater than 1'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the point of not allowing 0 and 1 as count. That just makes it harder to use this API programmatically with unexpected count
numbers.
throw new TypeError('`count` option should be a number greater than 1'); | |
throw new TypeError('The `count` option should be a positive integer or Infinity'); |
The count
option should also be documented for the above, that it accepts a positive integer or Infinity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, why have you removed the
article from the test name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so count >= 0
it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, why have you removed the article from the test name?
I felt it was moot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, just a shortcut? :P
Co-Authored-By: szmarczak <36894700+szmarczak@users.noreply.github.com>
Perfect 👌 |
Nitpick: |
Has this feature released? I have encountered TypeError on |
No, not yet. Latest release was in June. |
It seems I just forgot to release it. Duuh. Done now. |
Fixes #3