Skip to content

Commit ddfdd71

Browse files
jacobheunvmx
authored andcommitted
fix: avoid sync callbacks in async code
1 parent 399dc0e commit ddfdd71

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/decision-engine/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class DecisionEngine {
7171
cb()
7272
})
7373
} else {
74-
cb()
74+
setImmediate(cb)
7575
}
7676
}, cb)
7777
}
@@ -179,7 +179,7 @@ class DecisionEngine {
179179
const ledger = this._findOrCreate(peerId)
180180

181181
if (msg.empty) {
182-
return cb()
182+
return setImmediate(cb)
183183
}
184184

185185
// If the message was a full wantlist clear the current one
@@ -190,7 +190,7 @@ class DecisionEngine {
190190
this._processBlocks(msg.blocks, ledger)
191191

192192
if (msg.wantlist.size === 0) {
193-
return cb()
193+
return setImmediate(cb)
194194
}
195195

196196
let cancels = []
@@ -219,7 +219,7 @@ class DecisionEngine {
219219
})
220220
}
221221

222-
_addWants (ledger, peerId, entries, cb) {
222+
_addWants (ledger, peerId, entries, callback) {
223223
each(entries, (entry, cb) => {
224224
// If we already have the block, serve it
225225
this.blockstore.has(entry.cid, (err, exists) => {
@@ -235,7 +235,7 @@ class DecisionEngine {
235235
})
236236
}, () => {
237237
this._outbox()
238-
cb()
238+
callback()
239239
})
240240
}
241241

src/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const waterfall = require('async/waterfall')
44
const reject = require('async/reject')
55
const each = require('async/each')
66
const series = require('async/series')
7+
const setImmediate = require('async/setImmediate')
78
const map = require('async/map')
89

910
const WantManager = require('./want-manager')
@@ -108,7 +109,7 @@ class Bitswap {
108109
(has, cb) => {
109110
this._updateReceiveCounters(peerId.toB58String(), block, has)
110111
if (has) {
111-
return cb()
112+
return setImmediate(cb)
112113
}
113114

114115
this._putBlock(block, cb)
@@ -146,7 +147,7 @@ class Bitswap {
146147
_putBlock (block, callback) {
147148
this.blockstore.put(block, (err) => {
148149
if (err) {
149-
return callback(err)
150+
return setImmediate(() => callback(err))
150151
}
151152

152153
this.notifications.hasBlock(block)
@@ -310,7 +311,7 @@ class Bitswap {
310311
(cb) => this.blockstore.has(block.cid, cb),
311312
(has, cb) => {
312313
if (has) {
313-
return cb()
314+
return setImmediate(cb)
314315
}
315316

316317
this._putBlock(block, cb)
@@ -333,7 +334,7 @@ class Bitswap {
333334
}, cb),
334335
(newBlocks, cb) => this.blockstore.putMany(newBlocks, (err) => {
335336
if (err) {
336-
return cb(err)
337+
return setImmediate(() => cb(err))
337338
}
338339

339340
newBlocks.forEach((block) => {
@@ -345,7 +346,7 @@ class Bitswap {
345346
}
346347
})
347348
})
348-
cb()
349+
setImmediate(cb)
349350
})
350351
], callback)
351352
}

src/types/message/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ BitswapMessage.deserialize = (raw, callback) => {
187187
if (decoded.payload.length > 0) {
188188
return each(decoded.payload, (p, cb) => {
189189
if (!p.prefix || !p.data) {
190-
cb()
190+
return setImmediate(cb)
191191
}
192192
const values = vd(p.prefix)
193193
const cidVersion = values[0]
@@ -203,7 +203,7 @@ BitswapMessage.deserialize = (raw, callback) => {
203203
try {
204204
cid = new CID(cidVersion, codecName[multicodec.toString('16')], hash)
205205
} catch (err) {
206-
return callback(err)
206+
return cb(err)
207207
}
208208

209209
msg.addBlock(new Block(p.data, cid))

0 commit comments

Comments
 (0)