Skip to content

Commit 4b453bd

Browse files
committed
REFACTOR: Promisify createSwarm
1 parent 754633e commit 4b453bd

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

server.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,17 @@ class Server extends EventEmitter {
146146
if (cb) cb(null);
147147
}
148148

149-
createSwarm (infoHash, cb) {
149+
createSwarm(infoHash) {
150150
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
151151

152-
process.nextTick(() => {
153-
const swarm = this.torrents[infoHash] = new Server.Swarm(infoHash, this)
154-
cb(null, swarm)
155-
})
152+
const createdSwarm = resolve => {
153+
process.nextTick(() => {
154+
const swarm = this.torrents[infoHash] = new Server.Swarm(infoHash, this)
155+
resolve(swarm)
156+
})
157+
}
158+
159+
return new Promise(createdSwarm)
156160
}
157161

158162
getSwarm(infoHash) {
@@ -167,20 +171,21 @@ class Server extends EventEmitter {
167171
return new Promise(gotSwarm)
168172
}
169173

170-
// Get existing swarm, or create one if one does not exist
174+
// Get existing swarm, or create one if one does not exist
171175
getOrCreateSwarm(params) {
172176
const gotOrCreatedSwarm = resolve => {
173177
const gotSwarm = swarm => {
174178
if (swarm) return resolve(swarm)
175179

176-
this.createSwarm(params.info_hash, (err, swarm) => {
177-
if (err) return resolve(err)
178-
resolve(swarm)
179-
})
180+
this.createSwarm(params.info_hash)
181+
.then(
182+
(swarm) => {
183+
resolve(swarm)
184+
})
180185
}
181186

182187

183-
this.getSwarm(params.info_hash)
188+
return this.getSwarm(params.info_hash)
184189
.then(gotSwarm)
185190
}
186191

0 commit comments

Comments
 (0)