1

I have two asynchronous operations. In the interval between them, the result values are not empty and they are used in the next asynron operation. The fact is that one variable with the result falls into the response object from the server, and the second is empty. In this situation: 'frequency' is array, 'lemms' is object.

try {

      const pr = new Promise(async (resolve, reject) => {

        try {

          const { frequency, lemms } = await freqAnalysis(tokenized, lang) 


          // frequency is visible in this place 
          const { result, chunks } = await getTesaurusData(frequency)
          
          // but not in this, i get lemms successfully
          resolve({ frequency, lemms, result, chunks })

        } catch (err) {

          reject(err.message)
        }

      })

      const { frequency, lemms, result, chunks} = await pr 

      const { tesauruses } = formTesaurusData(frequency, result, chunks)


      let options = {
        res,
        result: {
          frequency, // empty array
          lemms, // good!
          tesaurus: {
            tesauruses,
            totalWords: frequency.length || null,// null
          }
        },
        message: 'success',
        statusCode: 200
      }

      resp(options)
Lukashev
  • 13
  • 2
  • If you have two awaitable function, why put them in a promise? You can just.... await them, no? – Holger Will Dec 29 '18 at 10:31
  • [Never ever pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572) – Bergi Dec 29 '18 at 10:38
  • 1
    If `frequency` is an array and you can see it contain elements after it's been returned from `freqAnalysis(…)` but it is empty after the `getTesaurusData(frequency)` call, then the only conclusion is that `getTesaurusData` is broken and removes the elements. Please post the code of that function. – Bergi Dec 29 '18 at 10:41

0 Answers0