如何解决承诺{<pending>},即使我在节点获取后有一个.then
我在函数中有一个访存,并且能够调用它并获得响应。但是,如果我添加带有.then子句的console.log语句,则会看到“承诺未决”消息,如下面的输出所示。我还将调用一个例程,将JSON从一种格式转换为另一种格式。
样本输出:
Function: 2020-8-26-22-10-23
1) data.json() from Fetch From gettranslation REST service:
Promise { <pending> }
[Function: getFormattedTime] Back from call fetch
ReactTables(): Resulting DataTable=
[
{
"_id": "5f403f7e5036d7bdb0adcd09","masterLanguage": "en-US","masterPhrase": "Customers","localizedLanguage": "es-MX","localizedPhrase": "Clientes"
},etc...
我的目的是显示从服务返回的数据, 然后调用另一个函数来重新格式化数据,然后返回数据。
获取链接后,是否需要将更多.then语句链接在一起?
是的,有类似的问题,但没有一个与这个问题足够接近。 示例1:Why is my asynchronous function returning Promise { <pending> } instead of a value?的问题是他没有.then ...,但我有。
示例2:Promise still pending even though using .then()(这与我的问题类似,但答案是-1票,似乎用不同的风格重写了。)
示例3-有人说这个问题已经回答了这个问题:How do I return the response from an asynchronous call?,但就我而言,我可以返回一个响应,但无法显示数据或在返回之前调用函数。
function getDBLanguagesAndConvertToDataTable(fromLanguage,toLanguage) {
console.log("Function: " + getFormattedTime())
var url = 'http://localhost:3001/api/gettranslations/' + fromLanguage + "/" + toLanguage
var fetchOptions = {
method: 'GET',headers: {
'Accept': 'application/json',}
}
return fetch(url,fetchOptions)
.then(function(data) {
var fetchResult = data.json()
console.log("data.json() from Fetch From gettranslation REST service:")
// This show nothing,or Promise { <pending>> }
console.log(JSON.stringify(fetchResult))
// Once I see the results above,then I will continue with the conversion...
//var tmpDataTable = convertMongoLanguagesToDataTable(fetchResult )
//return tmpDataTable
return fetchResult
})
.catch(function(err){
console.log("Fetch Error:" + err)
throw new Error(err)
})
}
console.log("------- Test with Fetch/Get/FromWebservice -------")
console.log(getFormattedTime())
var fromLanguage = 'en-US'
var toLanguage = 'es-MX'
getDBLanguagesAndConvertToDataTable(fromLanguage,toLanguage)
.then(function (dataTable) {
console.log(getFormattedTime," Back from call fetch")
var strDataTable = JSON.stringify(dataTable,null,3);
console.log("ReactTables(): Resulting DataTable=")
console.log(strDataTable)
console.log(getFormattedTime(),"The end of tests ")
})
.catch(function (err){
console.log("call function getDBLanguagesAndConvertToDataTable error:",getFormattedTime(),"The end of tests ")
})
convertMongoLanguagesToDataTable已单独测试,未读取。当我确实调用convertMongoLanguagesToDataTable时,它的行为好像传递给它的数据为空,这又回到了我认为的“ Pending Promise”问题。现在,我可以在函数getDBLanguagesAndConvertToDataTable中调用该函数,但这不是我想要的。
例如,我通过反转注释来更改这三行:
var tmpDataTable = convertMongoLanguagesToDataTable(fetchResult )
return tmpDataTable
//return fetchResult
我得到以下输出:
ReactTables(): Resulting DataTable=
{
"headerRow": [
"SourceLanguage","SourcePhrase","TargetLanguage","TargetPhrase","ID"
],"footerRow": [
"SourceLanguage","dataRows": []
}
2020-8-26-22-34-30 The end of tests
dataRows数组为空(不是在不进行读取的情况下单独测试它的情况,即将变量设置为相似的JSON对象时不是这样)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。