Skip to content

Commit d463ba0

Browse files
committed
REFACTOR: Async swarms
1 parent 4b453bd commit d463ba0

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

server.js

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

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

152152
const createdSwarm = resolve => {
@@ -159,7 +159,7 @@ class Server extends EventEmitter {
159159
return new Promise(createdSwarm)
160160
}
161161

162-
getSwarm(infoHash) {
162+
async getSwarm(infoHash) {
163163
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
164164

165165
const gotSwarm = resolve => {
@@ -172,24 +172,11 @@ class Server extends EventEmitter {
172172
}
173173

174174
// Get existing swarm, or create one if one does not exist
175-
getOrCreateSwarm(params) {
176-
const gotOrCreatedSwarm = resolve => {
177-
const gotSwarm = swarm => {
178-
if (swarm) return resolve(swarm)
179-
180-
this.createSwarm(params.info_hash)
181-
.then(
182-
(swarm) => {
183-
resolve(swarm)
184-
})
185-
}
186-
187-
188-
return this.getSwarm(params.info_hash)
189-
.then(gotSwarm)
190-
}
175+
async getOrCreateSwarm(params) {
176+
const swarm = await this.getSwarm(params.info_hash)
177+
|| await this.createSwarm(params.info_hash)
191178

192-
return new Promise(gotOrCreatedSwarm)
179+
return swarm
193180
}
194181

195182
_onRequest (params, cb) {

0 commit comments

Comments
 (0)