2017-09-15 96 views
3

我想在高速路由中使用异步/等待,不能让它正常工作。我看过几篇SO帖子,看来我做得很对。异步等待退货承诺<pending>

助手:

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) 
    }); 
    } 
} 

内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() 

我本来有一个问题,在我的助手使用回调,并将其转化为承诺,因为我学会了异步函数返回的承诺。但是用这段代码,我仍然不能返回我想要的值。我做错了什么,我该如何解决它?

+0

*“但是通过这段代码,我仍然不能返回我想要的值。”*您想从哪里返回哪个值?正如你所说,“异步”函数会返回承诺。 –

+0

''helpers.personalityInsights($ {facebookR} $ {twitterR})''最终是我想返回 想法是我使用异步等待以特定的顺序传递值,然后最后等待函数返回最后结果。 – Quesofat

+5

你想知道为什么'main'会返回一个未决的promise吗?再一次,你自己说过:'异步'函数返回承诺。该承诺最终将解决任何'helpers.personalityInsights('$ {facebookR} $ {twitterR}')'解决的问题。你提取一个promise的值,你必须通过'.then',即'main()。then(result => console.log(result))'传递一个回调。 –

回答

0
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) 
    }); 
    } 

你应该从personalityInsights函数返回一些值。