-
Notifications
You must be signed in to change notification settings - Fork 12
Refactor datastore core to use async/await instead of callbacks #17
Conversation
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.
Nice work @zcstarr, the code is so much nicer with async/await 👍
I made a couple of small suggestions in the code but overall looks really good. In general I would suggest
- using streaming-iterables functions instead of rolling our own
- I don't think you need to explicitly mark a function as
async
if it already returns a Promise - In some places you can use err-code so that you can then refer to that code explicitly in the tests (see eg https://github.com/libp2p/js-libp2p-kad-dht/blob/master/test/kad-dht.spec.js#L403)
async open () /* : void */ { | ||
try { | ||
await (this.stores.map((store) => store.open())) | ||
} catch (err) { |
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 know this wasn't part of the original code, but I wonder if we should log the error here?
cb() | ||
}) | ||
}, callback) | ||
async has (key /* : Key */) /* : Promise<bool> */ { |
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.
Would it make sense to do this in parallel? It's a little tricky because you want to return true as soon as you get a true response from a store, but it should be possible with a function from streaming-iterables (maybe transform()?)
each(batches, (b, cb) => { | ||
b.commit(cb) | ||
}, callback) | ||
commit: async () /* : Promise<void> */ => { |
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.
Should these run in parallel?
@@ -0,0 +1,9 @@ | |||
'use strict' |
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.
You can use streaming-iterables collect()
I refactored the package to use async/await as per ipfs/js-ipfs#1670. I added a helper function many that might make sense to move into datastore-interface util, if its generally useful to round robin async iterables. Let me know what you think, happy to jump in and patch up, any feedback greatly appreciated!