Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
refactor: call callbacks inside setImmediate
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
lidel committed Jul 30, 2019
1 parent d9ebf92 commit 89e957f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
14 changes: 4 additions & 10 deletions src/core/runtime/dns-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ const _httpQueue = new PQueue({ concurrency: 4 })
function unpackResponse (domain, response, callback) {
if (response.Path) {
return callback(null, response.Path)
} else {
const err = new Error(response.Message)
return callback(err)
}
return callback(new Error(response.Message))
}

module.exports = (domain, opts, callback) => {
Expand Down Expand Up @@ -49,15 +47,11 @@ module.exports = (domain, opts, callback) => {
})

_httpQueue.add(() => fetch(url, { mode: 'cors' })
.then((response) => {
return response.json()
})
.then((response) => response.json())
.then((response) => {
cache.set(query, response, ttl)
return unpackResponse(domain, response, callback)
})
.catch((error) => {
callback(error)
setImmediate(() => unpackResponse(domain, response, callback))
})
.catch((err) => setImmediate(() => callback(err)))
)
}
4 changes: 2 additions & 2 deletions src/core/runtime/preload-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ module.exports = function preload (url, callback) {
log.error('failed to preload', url, res.status, res.statusText)
throw new Error(`failed to preload ${url}`)
}
return res.text()
setImmediate(callback)
})
).then(() => callback(), callback)
).catch((err) => setImmediate(() => callback(err)))

return {
cancel: () => controller.abort()
Expand Down

0 comments on commit 89e957f

Please sign in to comment.