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
pull/4198/head
Joas Souza 2020-07-23 16:47:59 -03:00 committed by GitHub
parent ca6da2c724
commit 0835bfeabb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 6 deletions

View File

@ -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 */