gpt4 book ai didi

javascript - 异步等待返回 Promise

转载 作者:搜寻专家 更新时间:2023-11-01 00:05:08 24 4
gpt4 key购买 nike

我正在尝试在快速路由中使用 async/await,但无法使其正常工作。我看过几个 SO 帖子,看来我做对了。

帮助者:

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

我最初在我的助手中使用回调并将它们转换为 promise 时遇到问题,因为我了解到异步函数返回 promise 。但是使用这段代码,我仍然无法返回我想要的值。我做错了什么,我该如何解决?

最佳答案

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 函数返回一些值。

关于javascript - 异步等待返回 Promise <pending>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46247613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com