refactor: use .map to return promises

This commit is contained in:
Barış Soner Uşaklı
2021-12-15 12:49:50 -05:00
parent dda7c0753e
commit d4f3ee67fa

View File

@@ -136,33 +136,14 @@ define('forum/flags/list', [
if (subselector) { if (subselector) {
const action = subselector.getAttribute('data-action'); const action = subselector.getAttribute('data-action');
const flagIds = Flags.getSelected(); const flagIds = Flags.getSelected();
const promises = []; const promises = flagIds.map((flagId) => {
const data = {};
// TODO: this can be better done with flagIds.map to return promises if (action === 'bulk-assign') {
flagIds.forEach(function (flagId) { data.assignee = app.user.uid;
promises.push(new Promise(function (resolve, reject) { } else if (action === 'bulk-mark-resolved') {
const handler = function (err) { data.state = 'resolved';
if (err) {
reject(err);
} }
return api.put(`/flags/${flagId}`, data);
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;
}
}));
}); });
Promise.allSettled(promises).then(function (results) { Promise.allSettled(promises).then(function (results) {