-1

I want to execute the code sequentially, first the foreach loop needs to executed and then it should call the getHistory() method. Thanks in advance

const start = async()=>{


         await this.currentContent.forEach(async currentContent => {
          if (!currentContent.footnote) {


            let tempContent: any = await this._http.get(`VersionHistory/ContentAuditTrail/${currentContent.id}/${this.newDatetimeSelection}`).toPromise();
            if (tempContent){
              console.info('end time call : raw content');
              currentContent.rawContent = tempContent.value;

            }
            // content.rawContent = await this._http.get(`VersionHistory/ContentAuditTrail/${c.id}/${this.oldDatetimeSelection}`).toPromise();
          } else {

            let newcontent: any = await this._http.get(`VersionHistory/ContentFootnoteAuditTrail/${currentContent.footnote.id}/${this.newDatetimeSelection}`).toPromise();
            if (newcontent) {
              console.info('end time call : footnote content');
              currentContent.footnote.footnote = newcontent.value;

            }
          }
        })
        //setTimeout(() => {
          console.info('end time call');
          this.getHistory();
        //}, 4000);
      }

      start();
  • You are giving the `forEach` an async function. So even if you say `await someArray.forEach` it won't wait for all the async callbacks. – VLAZ Nov 05 '19 at 08:32
  • @VLAZ I am new to javascript and typescript. Could you pls help me with this code ? – taashibhulani Nov 05 '19 at 09:17

1 Answers1

0

Something like this -

Promise.all(this.currentContent.map(##first part of your code##))
    .then(() => {
        console.info('end time call');
        this.getHistory();
})