Skip to content

Instantly share code, notes, and snippets.

@gemmadlou
Created October 8, 2015 19:42
Show Gist options
  • Save gemmadlou/0f9b5b1f7b3f4f5c6375 to your computer and use it in GitHub Desktop.
Save gemmadlou/0f9b5b1f7b3f4f5c6375 to your computer and use it in GitHub Desktop.
Chaining Promises And Recursive Functions
var loopData = [
// Array of Objects
]
EventRepo.GetEventById(req.body.event_id.toString())
.then(function (data: { rows: {}[] }) {
if (data.rows.length === 0)
throw new EventRepoError('Event with id:\'' + req.body.event_id + '\' does not exist');
return loopData;
}).map(function (rowData: { id: string }) {
return EventRepo.GetDataById(req.body.event_id, rowData.id);
}).map(function (data: { rows: { id: string, status: number, name: string, rowData_type: string, parent_id: string }[] }) {
if (data.rows.length === 0) {
// return Promise of Database call
} else {
// return Promise of Database call
}
}).then(function (data) {
return res.status(200).json({ 'status': 'success', message: 'Import has completed successfully'});
})
.catch(EventRepoError, function (e) {
return res.status(400).json({ message: e.message });
}).catch(function (err) {
return res.status(500).json({ message: 'Error', error: err.message });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment