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
JS Driver: promise friendly cursor.eachAsync()
#4784
Conversation
If I read the coffeescript correctly this method will sequentialize all iterations by invoking the next callback when the previous one is resolved. Isn't it possible to execute all iterations in parallel with a |
@jorrit that would be easier to do now, at least: var results = [];
cursor.eachAsync(function (row) {
results.push(asyncRowHandler(row.id));
// as long as we don't return the promise
// the next row will be read immediately
})
.then(function () {
return Promise.all(results).then(function (data) { /* ... */ });
}); |
Personally I see no benefit in eachAsync supporting a callback that returns Promises when it serializes these promises. Maybe make it configurable with an options parameter? |
はい |
I would make me (the testing guy) particularly happy if you added appropriate tests to |
return p | ||
else | ||
throw new err.ReqlDriverError "First argument to `next` must be a function or undefined." | ||
return Promise.fromNode(fn).nodeify(cb) |
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.
Damn, that is a nice cleanup. I need to do that in the other places we do this manually...
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.
@deontologician thanks! In my last commit I went ahead and refactored cursor.toArray
into a one-liner as well.
@larkost I already copied the |
I like this interface. It seems to make the common case easy. When I think of iteration with each, I think of them executing in order. Given that you can just collect them and run them asynchronously if you want, it seems ok not to provide an option. It looks good to me overall. I've played with the code a bit and it works like I'd expect. Since this is an api change, I'd like to let it breath for a bit before merging it. Maybe get some opinions from other js guys that hang around here like @neumino and @joaojeronimo ... |
@larkost just added tests covering all the cases outlined in my opening comment and added a "stop iteration early" test to @deontologician somewhat unrelated to this PR, the original |
Looks like it was part of removing |
Ok, that makes me pretty certain it was a bug. We weren't even checking the result of that last (unintentional) iteration, so I don't see how it could impact anything. |
@deontologician -- Adding a new method seems reasonable to me and I can see how useful it can be. Maybe there's a better name than |
Ok, I'm ready to merge this, but I was wondering what people thought of the name "eachPromise" for the name. I think I agree with @neumino that since both each implementations are async, it might be misleading to call it eachAsync. @marshall007 thoughts? (or a better name) |
@deontologician I think at the next opportunity to introduce breaking changes (i.e. 2.2) we should consider changing the current signature of cursor.each(function(err, row), function()) // current
cursor.each(function(row, cb), function(err)) // new callback-style
cursor.each(function(row)) // new promise-style I wouldn't mind waiting to merge all this at once under https://github.com/petkaantonov/bluebird/blob/master/API.md#promisification Notice examples like |
Hmm. Alright, in that case I think we should just merge this as-is then. Breaking backwards compatibility in something this core is something I would expect in a major release (like 3.0). I think adding the extra method is a good compromise in the meantime. |
JS Driver: promise friendly `cursor.eachAsync()`
branch: larkost/4787-yaml-parser review: CR 3237
Fixes #4780. Unfortunately, there was no way to implement this as an overload to the existing
cursor.each()
since there's no way to disambiguate the argument style.Here's some example usage: