Javascript 我不知道';尽管使用了wait,我还是得不到我承诺的结果

Javascript 我不知道';尽管使用了wait,我还是得不到我承诺的结果,javascript,android,ios,react-native,request,Javascript,Android,Ios,React Native,Request,我的函数如下所示: async function getPlaceForecast(lat, long) { //var response; var day5forecast = 'http://api.openweathermap.org/data/2.5/forecast?lat=' + lat + '&lon=' + long + '&appid=' + apikey; try { const request = new Request(tmp2, {

我的函数如下所示:

async function getPlaceForecast(lat, long) {
//var response;
var day5forecast = 'http://api.openweathermap.org/data/2.5/forecast?lat=' + lat + '&lon=' + long + '&appid=' + apikey;
try {
    const request = new Request(tmp2, {
        method: 'get'
    })
    let response = await fetch(request)
        .then(value => {
            console.log("than", value.json());
            return value//.json();
        })
        .catch(function (error) {
            console.error(error);
        });
}
catch{

}
//return response;
}

结果是:

'than', { _40: 0, _65: 0, _55: null, _72: null }
这是不使用.json()的结果:

'than',{type:'default',
现状:200,
好的,没错,
状态文本:未定义,
标题:
{地图:
{'access control allow methods':'GET,POST',
“访问控制允许凭据”:“true”,
“访问控制允许来源”:“*”,
“x-cache-key”:“/data/2.5/forecast?lat=28.35&lon=-14.05”,
连接:“保持活力”,
“内容长度”:“14383”,
“内容类型”:“应用程序/json;字符集=utf-8”,
服务器:'openresty'}},
网址:'http://api.openweathermap.org/data/2.5/forecast?lat=28.349414&lon=-14.046311&appid=',
_bodyInit:
{u数据:
{大小:14383,
偏移量:0,
blobId:'2dbf82c5-5d28-47db-9034-7f239bf8290a'},
_bodyBlob:
{u数据:
{大小:14383,
偏移量:0,
blobId:'2dbf82c5-5d28-47db-9034-7f239bf8290a'}}

我做错了什么?

您正在记录由
json()
返回的承诺,而不是承诺的履行值

以下是如何实现该功能:

async function getPlaceForecast(lat, long) {
    const day5forecast = 'http://api.openweathermap.org/data/2.5/forecast?lat=' + lat + '&lon=' + long + '&appid=' + apikey;

    const request = new Request(tmp2, {
//                              ^^^^−−−−−−−−− Should this be `day5forecast` ??
//                                           `day5forecast` isn't used otherwise...
        method: 'get'
    })
    let response = await fetch(request);
    if (!response.ok) {
        throw new Error("HTTP error " + response.status);
    }
    return response.json();
}
注意我已经删除了
try
/
catch
es。将其留给调用方处理,以便他们知道
getPlaceForecast
是成功还是失败

您可能想知道结尾缺少
wait
returnresponse.json();
)。您不需要它,因为
async
函数总是返回承诺<代码>返回响应.json()将
async
函数的承诺解析为
json()
返回的承诺(因此它将根据
json()
中的承诺发生的情况来实现或拒绝)。您可以在那里使用
await
return await response.json();
),它是无害的,但您不需要它



特别是:A)由于ES2019的变化,在现代浏览器中它是完全无害的;在理论上发生改变之前,总体实现/拒绝延迟了一个额外的异步“勾号”(如果返回的承诺不是本机承诺,这种情况仍然会发生);B) 即使有额外的滴答声,它也基本无害。:-)

url变量“day5forecast”在哪里使用?
async function getPlaceForecast(lat, long) {
    const day5forecast = 'http://api.openweathermap.org/data/2.5/forecast?lat=' + lat + '&lon=' + long + '&appid=' + apikey;

    const request = new Request(tmp2, {
//                              ^^^^−−−−−−−−− Should this be `day5forecast` ??
//                                           `day5forecast` isn't used otherwise...
        method: 'get'
    })
    let response = await fetch(request);
    if (!response.ok) {
        throw new Error("HTTP error " + response.status);
    }
    return response.json();
}