How to deal with asynchrony in API calls

답변함


2022년 9월 28일에 게시됨

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

댓글 0개

로그인하세요.

원하는 정보를 못 찾으셨나요?

새 게시물