mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-13 17:21:21 +00:00
fix: promisified methods (#398)
* chore: update ws rendezvous dep test(fix): fix tests with latest ws rendezvous server * fix: promisification of libp2p methods test: add tests to verify promisify support until async/await endeavor is complete * chore: fix linting
This commit is contained in:
24
src/index.js
24
src/index.js
@ -187,6 +187,11 @@ class Libp2p extends EventEmitter {
|
||||
})
|
||||
|
||||
this._peerDiscovered = this._peerDiscovered.bind(this)
|
||||
|
||||
// promisify all instance methods
|
||||
;['start', 'stop', 'dial', 'dialProtocol', 'dialFSM', 'hangUp', 'ping'].forEach(method => {
|
||||
this[method] = promisify(this[method], { context: this })
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
@ -295,6 +300,13 @@ class Libp2p extends EventEmitter {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the given peer
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
hangUp (peer, callback) {
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
@ -303,6 +315,13 @@ class Libp2p extends EventEmitter {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Pings the provided peer
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
|
||||
* @param {function(Error, Ping)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
ping (peer, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(notStarted('ping', this.state._state))
|
||||
@ -550,11 +569,6 @@ class Libp2p extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
// promisify all instance methods
|
||||
['start', 'stop', 'dial', 'dialProtocol', 'dialFSM', 'hangUp', 'ping'].forEach(method => {
|
||||
Libp2p[method] = promisify(Libp2p[method])
|
||||
})
|
||||
|
||||
module.exports = Libp2p
|
||||
/**
|
||||
* Like `new Libp2p(options)` except it will create a `PeerInfo`
|
||||
|
Reference in New Issue
Block a user