简体   繁体   English

Async Await返回Promise <pending>

[英]Async Await Returns Promise <pending>

I am trying to use async / await in an express route and cant get it to work correctly. 我试图在快速路由中使用async / await并且无法使其正常工作。 I have looked at several SO posts and it seems I'm doing it right. 我看过几篇SO帖子,看来我做得对。

Helpers: 助手:

module.exports = {

 facebook: function(userId, token) {
   return new Promise(resolver)
   function resolver(resolve, reject) {
      graph.get(`${userId}/feed?access_token=${token}`, function(err, res) {
         if (err) {
            if(res.paging && res.paging.next) {
              graph.get(res.paging.next, function(err2, res2) {

                var arr = []

                res.data.forEach(function(e) {
                  if (e.hasOwnProperty('message')) {
                    arr.push(e.message) 
                  } else if (e.hasOwnProperty('story')) {
                    arr.push(e.story)
                  } else {
                    console.log('something is not here')
                  }
                }) 

                res2.data.forEach(function(e) {
                  if (e.hasOwnProperty('message')) {
                  arr.push(e.message) 
                  } else if (e.hasOwnProperty('story')) {
                    arr.push(e.story)
                  } else {
                    console.log('something is not here')
                  }
                })
                console.log(arr)
                resolve(arr.toString())

              })

            }
         } 
      })
   }      

  },
  twitter: function(twittername) {
    return new Promise(resolver) 
    function resolver(resolve, reject) {
      T.get('search/tweets', { q: `from:${twittername}`, count: 500 }, function(err, data, response) {
        if (err) {
          reject(err)
          console.log(err)
        } else {
          data.statuses.forEach(function(e) {
          arr.push(e.text)
        })

      }
      resolve(arr.toString())
    })

    }

    console.log(arr)
     return arr.toString()
},
personalityInsights: function (fullString) { 
  console.log('fullString', fullString)
  personality_insights.profile({
      text: fullString,
      consumption_preferences: true
    },
    function (err, response) {
      if (err) {console.log(err)}
      else console.log(response)
    });
  }
}

Inside Route.js 在Route.js内

 async function main() {
    try {
       facebookR = await helpers.facebook(userId, accessToken)
       twitterR = await helpers.twitter(twittername)
       return await helpers.personalityInsights(`${facebookR} ${twitterR}`)
    } catch (e) {
      console.log('err main', e)
    }
  }

 main()

I originally had an issue with using callbacks in my helpers and converted them to promises because I learned Async functions return promises. 我最初在我的助手中使用回调并将它们转换为promises是一个问题,因为我学会了异步函数返回promises。 But with this code, I still cant return the values I want. 但是使用这段代码,我仍然无法返回我想要的值。 What am I doing wrong and how can I fix it? 我做错了什么,我该如何解决?

 personalityInsights: function (fullString) { console.log('fullString', fullString) personality_insights.profile({ text: fullString, consumption_preferences: true }, function (err, response) { if (err) {console.log(err)} else console.log(response) }); } 

You should be returning some values from personalityInsights function. 您应该从personalityInsights函数返回一些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM