levelup: get() requires key and callback arguments - no Promise?

510 Views Asked by At

levelup docs say claim that get(), put(), etc will return a Promise if called without a callback: https://github.com/Level/levelup#promises

I have the following code:

    db.get(gameName).then(JSON.parse).then(

but my then() functions never get called. The code runs without crashing but I get the following message in the console:

get() requires key and callback arguments

Am I missing something? Maybe a dependency? I am willing to wrap get() and put() in my own Promises but it seems silly to rewrite functionality like that if it's already there.

1

There are 1 best solutions below

0
On

Thanks to Jaromanda X and Greg Hornby for the tips. Indeed, levelup does not include Promises yet. But in the meantime, here's a solution using promisify, which is now native to node:

const dbNoPromise = levelup('./app-db');
const {promisify} = require('util');
const db = { get : promisify(dbNoPromise.get.bind(dbNoPromise)),
         put : promisify(dbNoPromise.put.bind(dbNoPromise)),
         del : promisify(dbNoPromise.del.bind(dbNoPromise)) };