Búsquedas recientes
No hay búsquedas recientes
How to deal with asynchrony in API calls
Publicado 28 sept 2022
Hello All,
I am writing an app to bulk delete users. However, users that have un-closed tickets cannot be deleted. I can run an API call to close their tickets, then run the call to delete the user, but because the calls run asynchronously the second call executes before the first has finished -- so the users are not deleted (as the tickets have not yet been closed).
To mitigate this, I nested one API call within the other -- something like this:
function closeTicketsAndDeleteUsers(arrayTicketIDs,arrayUserIDs)
{
// close tickets
API = "/api/v2/tickets/update_many.json?ids=" + arrayTicketIDs;
myURL = encodeURI(API);
Var settings = {
type: 'PUT',
url: myURL
};
client.request(settings).then(
function(data) {
// success close tickets -- now delete users
API = "/api/v2/users/destroy_many.json?ids=" + arrayUserIDs;
myURL = encodeURI(API);
Var settings = {
type: 'DELETE',
url: myURL
};
client.request(settings)
},
function(response) {
showError(response);
},
);
}
However, this still does not work. It successfully deletes the tickets but does not delete the user. I think this may be because the 'delete tickets' call puts the tickets in a queue, and they are still not deleted by the time that the 'delete users' call is executed. The users are successfully deleted only if there are no tickets for them.
Can anyone advise how to achieve my aim of both closing tickets and deleting users within a single process?
Thanks,
Simon.
0
3 comentarios
SGL
Just want to add:
I utilised an asynchronous function to get the status of the delete, as suggested by Jack above.
I added an asynchronous 'sleep' function to pause between calls to the status url, so that the rate limiter should not be triggered.
This solved the problem.
1
SGL
Hi Jack,
Thanks for your suggestion. I think this could fix my issue.
I wondered about defining async functions (particularly as there is an 'await' command) but it looked like these were more to allow asynchronous execution rather than the sequential execution that I am after.
Thanks,
Simon.
0
Jack
So the Update Many Tickets endpoint actually returns a job status object, so you are sort of right in thinking the call to that endpoint will put the tickets in a "queue" as it will delete them one by one and the job status will update.
You'll want to do something like this:
It may also be beneficial to look into async functions.
1
Iniciar sesión para dejar un comentario.