Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit cf08d94

Browse files
authored
Fix floating promise in constructor (closes #656) (#680)
1 parent bf076c5 commit cf08d94

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ The main entry point for creating a new `levelup` instance.
111111
- `db` must be an [`abstract-leveldown`](https://github.com/Level/abstract-leveldown) compliant store.
112112
- `options` is passed on to the underlying store when opened and is specific to the type of store being used
113113

114-
Calling `levelup(db)` will also open the underlying store. This is an asynchronous operation which will trigger your callback if you provide one. The callback should take the form `function (err, db) {}` where `db` is the `levelup` instance. If you don't provide a callback, any read & write operations are simply queued internally until the store is fully opened.
114+
Calling `levelup(db)` will also open the underlying store. This is an asynchronous operation which will trigger your callback if you provide one. The callback should take the form `function (err, db) {}` where `db` is the `levelup` instance. If you don't provide a callback, any read & write operations are simply queued internally until the store is fully opened, unless it fails to open, in which case an `error` event will be emitted.
115115

116116
This leads to two alternative ways of managing a `levelup` instance:
117117

@@ -487,6 +487,7 @@ const main = async () => {
487487
| `ready` | Alias of `open` | - |
488488
| `closing` | Store is closing | - |
489489
| `closed` | Store has closed. | - |
490+
| `error` | An error occurred | `error` (Error) |
490491

491492
For example you can do:
492493

lib/levelup.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function LevelUP (db, options, callback) {
3131
}
3232

3333
var error
34+
var self = this
3435

3536
EventEmitter.call(this)
3637
this.setMaxListeners(Infinity)
@@ -55,7 +56,9 @@ function LevelUP (db, options, callback) {
5556
this.options = getOptions(options)
5657
this._db = db
5758
this.db = new DeferredLevelDOWN(db)
58-
this.open(callback)
59+
this.open(callback || function (err) {
60+
if (err) self.emit('error', err)
61+
})
5962

6063
// Create manifest based on deferred-leveldown's
6164
this.supports = supports(this.db.supports, {

test/init-test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,40 @@ module.exports = function (test, testCommon) {
3939
})
4040
})
4141

42+
test('Init & open(): error with callback', function (t) {
43+
t.plan(1)
44+
45+
var mem = memdown()
46+
mem._open = function (opts, cb) {
47+
process.nextTick(cb, new Error('from underlying store'))
48+
}
49+
50+
levelup(mem, function (err) {
51+
t.is(err.message, 'from underlying store')
52+
}).on('open', function () {
53+
t.fail('should not finish opening')
54+
}).on('error', function () {
55+
t.fail('should not emit error')
56+
})
57+
})
58+
59+
test('Init & open(): error without callback', function (t) {
60+
t.plan(1)
61+
62+
var mem = memdown()
63+
mem._open = function (opts, cb) {
64+
process.nextTick(cb, new Error('from underlying store'))
65+
}
66+
67+
levelup(mem)
68+
.on('open', function () {
69+
t.fail('should not finish opening')
70+
})
71+
.on('error', function (err) {
72+
t.is(err.message, 'from underlying store')
73+
})
74+
})
75+
4276
test('Init & open(): validate abstract-leveldown', function (t) {
4377
t.plan(1)
4478

0 commit comments

Comments
 (0)