Currently in my Ionic 2 project, I am facing an issue where two functions are executing one after another but the second function starts before the first one is completed. Both functions involve making API calls and I want to ensure that the first function finishes its execution entirely before moving on to the second function.
To address this issue, I have been advised to use Promises. I have simplified the code for better readability:
const first = () => {
self.pspService.post('/api/Conversation/GetPersonalCalendarData',
{
}, result => {
result.Data.forEach(lAppointment => {
// logic here
});
});
return new Promise((resolve, reject) => {
resolve();
});
};
const second = () => {
self.pspService.post('/api/Conversation/AddPersonalAppointment', {
}, result => {
// logic here
});
return new Promise((resolve, reject) => {
resolve();
});
};
first().then(() => {
return second();
});