neumino / rethinkdbdash

An advanced Node.js driver for RethinkDB with a connection pool, support for streams etc.
MIT License
848 stars 109 forks source link

Add async iterator interface to cursors #371

Open mxstbr opened 6 years ago

mxstbr commented 6 years ago

See #370, this patch adds a cursor.asyncIterator method which returns an async iterator:

const feed = yield r.db().table().changes().run();

const iterator = feed.asyncIterator();

for await (var change of iterator) {
  console.log(change);
}
console.log('Done!');

I added iterall as a dependency since I need to use it in the tests anyway, but if you want me to I can move it to the devDependencies and inline the $$asyncIterator polyfill snippet. (it's like 3 lines)

mxstbr commented 6 years ago

Wercker failed even though all tests passed 🤔

segphault commented 6 years ago

I came here to do this and was pleasantly surprised to find a PR already open. Now that async iterators are supported natively in node, this would definitely be nice to have, ideally without polyfill. Would it be possible to use the [Symbol.asyncIterator] protocol so that it's possible to pass the cursor itself directly into the for-await-of loop?

mxstbr commented 6 years ago

Would it be possible to use the [Symbol.asyncIterator] protocol so that it's possible to pass the cursor itself directly into the for-await-of loop?

Already possible!

thelinuxlich commented 6 years ago

@neumino ?

thelinuxlich commented 6 years ago

merged in https://github.com/RebirthDB/rebirthdb-js