locked
Typescript Question - Waiting for Api Response RRS feed

  • Question

  • User941905567 posted

    I have api call that I am making in Typescript. It immediately receives a response from the called api.  However,,also as part of the api endpoint being called,it will  call a stored procedure that inserts rows into a database. This stored procedure call is something that happens in the background is completely separate from the response that is given to the client.

    Then in my typescript I need a way to ensure the stored procedure has completed. Once that has completed I will then make a call to a second api endpoint to retrieve the rows that were inserted in the stored procedure call from the first api call. Question: Is there a way I can make my page wait for some time before making the second api call ? Any help would be greatly appreciated..

        searchForPolicies(): void {
             
                 
                this._apiBatchRunDetail.PostMonthlyCancelPolicyStartBatch().then(b => {
                    this._loading.loading('cancellations');
                    this.batchid = b['data']['id']
                    //I need to wait for a bit before making this call
                    let PoliciesToCancel = this._apiBatchRunDetail.GetCancellationDetailList(this.batchid).then(cancpols => {
                        this.monthlyCancPoliciesFront = cancpols;
                        this.countofmonthlyCancPoliciesFront = this.monthlyCancPoliciesFront.length;
    
                    });
    
            });
                      
        }

    Wednesday, November 28, 2018 5:29 PM

All replies

  • User283571144 posted

    Hi Jazzcatone,

    Is there a way I can make my page wait for some time before making the second api call ? Any help would be greatly appreciated..

    According to your description, I suggest you could try to use  async/await syntax in typescript.

    async function delay(ms: number) {
        return new Promise( resolve => setTimeout(resolve, ms) );
    }

    And call it

    await delay(300);

    Noitce: You could use await only inside async function.

    Best Regards,

    Brando

    Thursday, November 29, 2018 6:44 AM
  • User-474980206 posted
    You can use setTimeout to do a delay, but how do you know how long? You can poll for the status the SP
    Friday, November 30, 2018 3:23 PM