0

I have a function updateNodes(). My await to the getFont external function is not "waiting" to move to the next line so it's treating every font as not seen and making a request. I am not awaiting this correctly?

_allFontSets = {}

updatesNodes() {
   for (const [entity] of this._allEntities) {
       const textObj = this._allEntities.get(textEntity);

       const data = {
          ranges: textObj.map(async (styleData)=> {
              const font = styleData.font;
              const stringified = JSON.stringify(font);

              if (Boolean(this._allFontSets[stringified])) {
                   console.log("ALREADY SEEN FONT")
                   font = JSON.parse(this._allFontSets[stringified]);
              } else {
                   console.log("NEW FONT")
                  font = await getFont() //external service; 
                  this._allFontSets[stringified] = font;
              }
          }
       }

   }
}
sal3jfc
  • 517
  • 4
  • 14
  • What isn't being awaited is the array of promises being returned by `.map()`, so the top-level loop continues without them. – David Aug 31 '22 at 19:03
  • Do you need await, that is asynchronous code? Try this little test make a for loop, and make another function that does some random work, console inside the function and just after the function returns, see if the function blocks the for loop as you call the function repeatedly. – WPW Aug 31 '22 at 19:04
  • thanks. @David even if I wrap it in a Promise.all, like: `await Promise.all(ranges: textObj.map(async (styleData)=> {`. .....) ` , it still does the same thing though – sal3jfc Aug 31 '22 at 19:07
  • @sal3jfc: That example doesn't appear to be syntactically correct. And it's also worth pointing out that the `updatesNodes` is not `async`, so it can't internally `await` anything. If you want to use `await` then you need to make the function `async`. – David Aug 31 '22 at 19:10

0 Answers0