How to wait until all async calls are finished

Andurit

I've got NestJS application which interact with YoutubeAPI and load videos from it. One particular method is important and it's loadVideos from below. Method it self has multiple asyncs inside and I need to work with videoIdMap property once everything is finished

private loadVideos(
    playListId: string,
    channel: Channel,
    nextPageToken: string,
    stopLoadingOnVideoId: string,
  ) {
    const baseUrl = YoutubeService.VIDEO_URL_SNIPPET_BY_ID + playListId;
    const response = this.httpService
      .get(nextPageToken ? baseUrl + '&pageToken=' + nextPageToken : baseUrl)
      .pipe(map((response) => response.data));
    response.subscribe((data) => {
      data.items.forEach((item) => {
        if (stopLoadingOnVideoId && item.snippet.resourceId.videoId === stopLoadingOnVideoId) {
          return;
        }        
        this.prepareVideoEntity(item.snippet, channel).then((partialVideo) =>              
          this.videoService.create(partialVideo).then((video) => {     
            this.videoIdMap[video.youtubeId] = video.id;
          }),
        );
      });      
      if (data.nextPageToken) {        
        this.loadVideos(
          playListId,
          channel,
          data.nextPageToken,
          stopLoadingOnVideoId,
        );
      }
    });
  }

Ideal solution for me would be to make loadVideos async somehow so I can later do:

public methodWhichCallLoadVideos(): void {
  await loadVideos(playListId, channel, null, stopLoadingOnVideoId)
  // My code which have to be executed right after videos are loaded
}

Every solution I tried out end up with this.videoIdMap to be empty object or with compilation issue so any idea is more than welcome.

eol

You could switch to promises instead of Observables, thus turning the method into an async one that recurs as long as data has a nextPageToken:

private async loadVideos(
        playListId: string,
        channel: Channel,
        nextPageToken: string,
        stopLoadingOnVideoId: string,
    ) {
        const baseUrl = YoutubeService.VIDEO_URL_SNIPPET_BY_ID + playListId;
        const response = await this.httpService
            .get(nextPageToken ? url + '&pageToken=' + nextPageToken : url).toPromise();
        const { data } = response;
        for (const item of data.items) {
            if (stopLoadingOnVideoId && item.snippet.resourceId.videoId === stopLoadingOnVideoId) {
                continue;
            }
            const partialVideo = await this.prepareVideoEntity(item.snippet, channel);
            const video = await this.videoService.create(partialVideo)
            this.videoIdMap[video.youtubeId] = video.id;
        }
        if (data.nextPageToken) {
            await this.loadVideos(
                playListId,
                channel,
                data.nextPageToken,
                stopLoadingOnVideoId,
            );
        }
    }

In your caller you can then simply await loadVideos(...):

private async initVideoIdMap(...) {
  await this.loadVideos(...);
  // this.videoIdMap should be correctly populated at this point
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to wait until all NSOperations is finished?

From Dev

How to wait until all tasks are finished before running code

From Dev

How to wait until all tasks finished without blocking UI thread?

From Dev

How to pick an event listener that will let me wait until async.times is finished to run a function

From Dev

How to wait until an animation is finished in Swift?

From Dev

how to wait until a web request with HttpWebRequest is finished?

From Dev

How to wait until some jQuery methods are finished?

From Dev

How to wait until networking by Alamofire has finished?

From Dev

How to wait until my batch file is finished

From Dev

How to get jQuery to wait until an animation is finished?

From Dev

Android - How to wait until a SnapshotListener has finished?

From Dev

Why the main does not wait until the async method has finished?

From Dev

C# .Net - How to make application wait until all threads created in Library are finished

From Dev

Wait until function is finished

From Dev

ExecutorService's shutdown() doesn't wait until all threads will be finished

From Dev

Create threads in loop and wait until all threads finished/aborted

From Dev

Waiting until all ajax calls are finished not working with $.when

From Dev

How to make python wait until the previous task is finished?

From Dev

In NodeJs how can I wait until my http get is finished?

From Dev

How to make Gulp wait until dest file is finished?

From Dev

How do I make an Excel RefreshAll wait to close until finished?

From Dev

How to make python wait until the previous task is finished?

From Dev

How can I wait until NSXMLParserDelegate methods finished?

From Dev

How to wait until an http request is finished before moving on?

From Dev

How to make JS wait until protocol execution finished

From Dev

How to make a python Script wait until download has finished

From Dev

How to wait until canvas has finished re-rendering?

From Dev

Wait until forEach loop is finished?

From Dev

Wait until gobalEval has finished

Related Related

  1. 1

    How to wait until all NSOperations is finished?

  2. 2

    How to wait until all tasks are finished before running code

  3. 3

    How to wait until all tasks finished without blocking UI thread?

  4. 4

    How to pick an event listener that will let me wait until async.times is finished to run a function

  5. 5

    How to wait until an animation is finished in Swift?

  6. 6

    how to wait until a web request with HttpWebRequest is finished?

  7. 7

    How to wait until some jQuery methods are finished?

  8. 8

    How to wait until networking by Alamofire has finished?

  9. 9

    How to wait until my batch file is finished

  10. 10

    How to get jQuery to wait until an animation is finished?

  11. 11

    Android - How to wait until a SnapshotListener has finished?

  12. 12

    Why the main does not wait until the async method has finished?

  13. 13

    C# .Net - How to make application wait until all threads created in Library are finished

  14. 14

    Wait until function is finished

  15. 15

    ExecutorService's shutdown() doesn't wait until all threads will be finished

  16. 16

    Create threads in loop and wait until all threads finished/aborted

  17. 17

    Waiting until all ajax calls are finished not working with $.when

  18. 18

    How to make python wait until the previous task is finished?

  19. 19

    In NodeJs how can I wait until my http get is finished?

  20. 20

    How to make Gulp wait until dest file is finished?

  21. 21

    How do I make an Excel RefreshAll wait to close until finished?

  22. 22

    How to make python wait until the previous task is finished?

  23. 23

    How can I wait until NSXMLParserDelegate methods finished?

  24. 24

    How to wait until an http request is finished before moving on?

  25. 25

    How to make JS wait until protocol execution finished

  26. 26

    How to make a python Script wait until download has finished

  27. 27

    How to wait until canvas has finished re-rendering?

  28. 28

    Wait until forEach loop is finished?

  29. 29

    Wait until gobalEval has finished

HotTag

Archive