feat: promisify all api methods that accept callbacks (#381)

* feat: promisify all api methods that accept callbacks

This is a stop-gap until the full async/await migration can be
completed.  It means we can refactor tests of other modules that
depend on this module without having to mix async flow control
strategies.

N.b. some methods that were previously callable without callbacks
(e.g. `node.start()`, `node.stop()`, etc) now require callbacks
otherwise a promise is returned which, if rejected, can cause
`unhandledPromiseRejection` events and lead to memory leaks.

* docs: add a global note to the api about promisify

* fix: update the logic for unsubscribe

* test(fix): correct pubsub unsubscribe usage for api change

* test(fix): update content routing tests for latest delegate version
This commit is contained in:
Alex Potsides
2019-07-29 14:40:40 +01:00
committed by Jacob Heun
parent b4a70ea476
commit df6ef45a2d
11 changed files with 85 additions and 56 deletions

View File

@ -3,6 +3,7 @@
const tryEach = require('async/tryEach')
const parallel = require('async/parallel')
const errCode = require('err-code')
const promisify = require('promisify-es6')
module.exports = (node) => {
const routers = node._modules.contentRouting || []
@ -24,7 +25,7 @@ module.exports = (node) => {
* @param {function(Error, Result<Array>)} callback
* @returns {void}
*/
findProviders: (key, options, callback) => {
findProviders: promisify((key, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
@ -60,7 +61,7 @@ module.exports = (node) => {
results = results || []
callback(null, results)
})
},
}),
/**
* Iterates over all content routers in parallel to notify it is
@ -70,7 +71,7 @@ module.exports = (node) => {
* @param {function(Error)} callback
* @returns {void}
*/
provide: (key, callback) => {
provide: promisify((key, callback) => {
if (!routers.length) {
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
}
@ -78,6 +79,6 @@ module.exports = (node) => {
parallel(routers.map((router) => {
return (cb) => router.provide(key, cb)
}), callback)
}
})
}
}