2017-09-15 95 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函數返回一些值。