From 0835bfeabb4babd8ee55111e82462b9d8fd3dd57 Mon Sep 17 00:00:00 2001 From: Joas Souza Date: Thu, 23 Jul 2020 16:47:59 -0300 Subject: [PATCH] Bugfix: wait promise finish on hooks (#4194) This commit fixes the error of not waiting the async code to finish. As the forEach did not wait until the async code finish we may get a hook set up incorrectly. To fix it, we use an "Array.map" to iterate and wait the promises to be resolved and then returned --- src/static/js/pluginfw/hooks.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/static/js/pluginfw/hooks.js b/src/static/js/pluginfw/hooks.js index c7a25bcad..b2c2aaab2 100644 --- a/src/static/js/pluginfw/hooks.js +++ b/src/static/js/pluginfw/hooks.js @@ -82,17 +82,16 @@ async function aCallAll(hook_name, args, cb) { if (!cb) cb = function () {}; if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []); - var newArray = []; - // This should be a map. - await exports.plugins.hooks[hook_name].forEach(async function(hook, index){ - let test = await hookCallWrapper(hook, hook_name, args, function (res) { + var hooksPromises = exports.plugins.hooks[hook_name].map(async function(hook, index){ + return await hookCallWrapper(hook, hook_name, args, function (res) { return Promise.resolve(res); }); - newArray.push(test) }); + var result = await Promise.all(hooksPromises); + // after forEach - cb(null, _.flatten(newArray, true)); + cb(null, _.flatten(result, true)); } /* return a Promise if cb is not supplied */