holepunchto/hypercore

Async/Await for feed.get and other

xoryouyou opened this issue · 2 comments

I am currently trying out hypercore and was wondering why all feed functions are using callbacks.

For example https://github.com/hypercore-protocol/hypercore#const-id--feedgetindex-options-callback

To integrate hypercore in my project I've written a small Promise wrapper around feed.get.

Is there any async/await functionality in hypercore as of now? Since I couldn't find any.

Example:

var hypercore = require('hypercore')

async function get(feed, index) {
    return new Promise((resolve, reject) => {
        feed.get(index, (err, data) => {
            if (err) {
                return reject(err);
            }
            resolve(data);
        })
    });
}

async function main() {
   var feed = hypercore('./my-dataset', { valueEncoding: 'utf-8' })
    feed.append('hello')
    let result = await get(feed, 0);
    console.log("result", result);
}

main();

Next's major has promise support (wip), for now you can use the promisifier to do so: https://github.com/andrewosh/hypercore-promisifier

Ahh perfect thats the stuff I was looking for.