在所有连续的promise之后如何获得变量的值?

时间:2018-12-29 10:24:30

标签: javascript node.js

我有两个异步操作。在它们之间的间隔中,结果值不为空,并在下一次异步操作中使用它们。事实是,结果的第一个变量从服务器落入响应对象,而第二个为空。在这种情况下:“频率”是数组,“ lemms”是对象。

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)

0 个答案:

没有答案