-
-
Notifications
You must be signed in to change notification settings - Fork 60
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
Allow breaking from iterator #31
Comments
Couldn't we just document that the returned array will be incomplete if the user stops early? Yes, PR welcome. You can find inspiration for the TS types in https://github.com/sindresorhus/find-up/blob/master/index.d.ts (and testing and docs too, probably) |
But this sentence alone leaves room for uncertainty on what happens with the intermediate missing values, for example if we are mapping over |
There are two use-cases I can think of:
So either we can:
Any opinions? What would go with? |
At first thought, I would prefer 3, but for that we would have to return However, sparse arrays are usually frowned upon. For example, I remember reading somewhere that TypeScript made a design choice of pretending that sparse arrays don't exist, in order to vastly simplify something. That said, I prefer option 1 now, filling with undefineds when the user chooses the 'keep indexes' option. And if someone also needs to differentiate between Option 2 is also interesting, but I suspect that most users will not need the returned array at all if they're breaking early, so what it returns will be irrelevant most of the time, so I think having a single, short-named |
In short: I would go with a single |
I agree with your assessment. Let's go with that. I think we need a better name for that option though. |
🤔 What happens with the index in which the early stop happened? Does it become undefined as well? 🤔 I had a new idea. Instead of providing a simple await pMap(someArray, async element => {
// ...
return pMap.stop({
value: 123,
missingIndexes: {
collapse: false,
fillWith: 'i-was-not-computed'
}
});
});
//=> ['foo', 'i-was-not-computed', 'bar', 123, 'i-was-not-computed', 'baz']
// ^^^ this element caused the early stop |
Good idea 👍 |
Do we want to call |
Nice idea @Richienb! We could detect if each promise supports canceling and cancel them. |
Ideally, we would also support AbortController, but that can be done later. Just something to keep in mind. |
Also see the discussion in sindresorhus/p-times#1. |
If anyone wants to work on this, see the initial attempt in #36 |
Idea: |
Another idea: one use case I can think of for early stopping is an async version of Array#find. In that case, an API that returns both the item that caused the stop and then the other values could be useful. |
I wish I could stop the iteration early by returning something like
pMap.stop
(a Symbol) from the iterator.This is basically the same request as sindresorhus/p-each-series#3
For now, I have to throw an error on purpose just to break it. Thankfully, I don't need the return value of
p-map
anyway.But with this feature, the parts of the array that weren't computed yet could be set to
undefined
, orpMap.uncomputed
(another Symbol), or even be specified by avalueForUncomputedEarlyStop
option.Can I do a PR?
The text was updated successfully, but these errors were encountered: