From 2c29675defb4e09f2608d1391b19bb3e6e94451e Mon Sep 17 00:00:00 2001 From: tshemsedinov Date: Wed, 28 Jun 2017 19:07:39 +0300 Subject: [PATCH] lib: rewrite i-face introspection without promises PR-URL: https://github.com/metarhia/jstp/pull/245 Reviewed-By: Alexey Orlenko Reviewed-By: Denys Otrishko Reviewed-By: Mykola Bilochub --- lib/transport-common.js | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/lib/transport-common.js b/lib/transport-common.js index 7a343167..05b96c8d 100644 --- a/lib/transport-common.js +++ b/lib/transport-common.js @@ -68,25 +68,23 @@ const newConnectAndInspectFn = (connFactory, transportClass) => { const callback = common.extractCallback(options); connect(appName, client, ...options, (error, connection) => { if (error) return callback(error); - - Promise.all(interfaces.map(name => new Promise((resolve, reject) => { - connection.inspectInterface(name, (error, proxy) => { - if (error) { - reject(error); - } else { - resolve(proxy); + const proxies = Object.create(null); + let errored = false; + const len = interfaces.length; + let count = 0; + interfaces.forEach(name => connection.inspectInterface(name, + (error, proxy) => { + if (!errored) { + count++; + if (error) { + errored = true; + return callback(error, connection); + } + proxies[name] = proxy; + if (count === len) callback(null, connection, proxies); } - }); - }))).then((proxies) => { - const api = proxies.reduce((acc, proxy, idx) => { - const name = interfaces[idx]; - acc[name] = proxy; - return acc; - }, Object.create(null)); - callback(null, connection, api); - }).catch((error) => { - callback(error, connection); - }); + } + )); }); }; };