Skip to content

Instantly share code, notes, and snippets.

@MasterHans
Created October 18, 2022 18:13
Show Gist options
  • Save MasterHans/08f28687024ce72a1a4899eff78caa0f to your computer and use it in GitHub Desktop.
Save MasterHans/08f28687024ce72a1a4899eff78caa0f to your computer and use it in GitHub Desktop.
AngularJS Promise.all()
c.sendEmail = function () {
c.savingData = true;
if (_.isUndefined(c.model.toEmail) && model.templateId === 7) {
if (model.agreementIds.length === 1 && model.agreementIds[0] === 0) {
//get all employees
personnelEmployee.fetchAllWorkingEmployees()
.then(function (data) {
console.log(data);
prepareAndSend();
})
}
if (model.departmentIds.length === 1 && model.departmentIds[0] === 0) {
//get all employees from departments
// personnelEmployee.fetchAllWorkingEmployees()
// .then(function (data) {
// console.log(data);
// prepareAndSend();
// })
}
} else {
prepareAndSend();
}
};
function prepareAndSend() {
var emails = c.model.toEmail.split(/[,\s;]+/);
if (c.model.sendCopyToSender) {
emails.push('fake');
}
var promises = emails.map(function () {
return common.getEmailQueueToken().then(
function (resp) {
return resp.data;
});
});
Promise.all(promises).then(function (tokens) {
c.model.token = tokens.join(',');
send()
.then(function (data) {
c.savingData = false;
c.model.errors = [];
notification.success(data.shortMessage);
c.close();
})
.catch(function (response) {
c.savingData = false;
if (response.data) {
c.model.errors = response.data.errors;
}
throw response;
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment