#Explanation of Promises

6 messages · Page 1 of 1 (latest)

sturdy relic
#
 
 async retrieveLiveGames() {
        return new Promise((resolve, reject) => {
          const filterOptions = {
            start_game: 0,
          };
      
          const handleSourceTVGamesResponse = (sourceTVGamesResponse: any) => {
            console.log(sourceTVGamesResponse);
            resolve(sourceTVGamesResponse);
          };
      
          this.dotaClient.on('sourceTVGamesData', handleSourceTVGamesResponse);
      
          this.dotaClient.requestSourceTVGames(filterOptions, (err: any, response: any) => {
            if (err) {
              console.error(err);
              reject(err);
              return;
            }
            
          });
        });
      }

how does this promise resolve or reject i dont unserstand? it works fine resolving but i dont know how it reaches that point 😓

urban gorge
#

The outside async is useless, but to answer your question, the promise waits until resolve or reject is called. Here, in your success handler (sourceTVGamesData) or error handler (if branch of your request callback)

sturdy relic
#

Or is it because its .on() and this is a event listener so it will come back to it when it detects changes?

urban gorge
#

You register the handler on a specific event (.on), then you call the function that will trigger this event (request). The handler being registered, it will be called for this event

sturdy relic
#

I see