From d4f3ee67fa26febe5c11302809e30e7aad3c8db0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 15 Dec 2021 12:49:50 -0500 Subject: [PATCH] refactor: use .map to return promises --- public/src/client/flags/list.js | 35 ++++++++------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/public/src/client/flags/list.js b/public/src/client/flags/list.js index dc942a578d..f2e15cedab 100644 --- a/public/src/client/flags/list.js +++ b/public/src/client/flags/list.js @@ -136,33 +136,14 @@ define('forum/flags/list', [ if (subselector) { const action = subselector.getAttribute('data-action'); const flagIds = Flags.getSelected(); - const promises = []; - - // TODO: this can be better done with flagIds.map to return promises - flagIds.forEach(function (flagId) { - promises.push(new Promise(function (resolve, reject) { - const handler = function (err) { - if (err) { - reject(err); - } - - resolve(arguments[1]); - }; - - switch (action) { - case 'bulk-assign': - api.put(`/flags/${flagId}`, { - assignee: app.user.uid, - }, handler); - break; - - case 'bulk-mark-resolved': - api.put(`/flags/${flagId}`, { - state: 'resolved', - }, handler); - break; - } - })); + const promises = flagIds.map((flagId) => { + const data = {}; + if (action === 'bulk-assign') { + data.assignee = app.user.uid; + } else if (action === 'bulk-mark-resolved') { + data.state = 'resolved'; + } + return api.put(`/flags/${flagId}`, data); }); Promise.allSettled(promises).then(function (results) {