Javascript 无法从promise获取正确的数据
大家好,我正在使用wait关键字等待异步调用返回。我的url是一个get url,所以如果我从浏览器调用它,就会返回一个json。问题是,当我试图从代码中获取数据时,它会返回一个承诺,但我不知道如何从该承诺中获取数据响应Javascript 无法从promise获取正确的数据,javascript,json,async-await,ecmascript-2017,Javascript,Json,Async Await,Ecmascript 2017,大家好,我正在使用wait关键字等待异步调用返回。我的url是一个get url,所以如果我从浏览器调用它,就会返回一个json。问题是,当我试图从代码中获取数据时,它会返回一个承诺,但我不知道如何从该承诺中获取数据响应 getCustomerById: async (id) => { try { const response = await fetch("http://www.ready.buzlin.com/buzlinApp/customer/getCustom
getCustomerById: async (id) => {
try {
const response = await fetch("http://www.ready.buzlin.com/buzlinApp/customer/getCustomerById.php?k=xxx&id=xxx");
// console.log("Response Returned: "+ JSON.stringify(response));
// resolve(response.json());
debugger;
return response.json();
} catch (err) {
}
}
这就是json.reponse返回给我的结果
谁能告诉我我做错了什么。谢谢取回。。。返回一个承诺,您使用wait来打开该承诺,这很好。但是response.json也会返回一个承诺,所以您可能还想在上面使用wait:
const response = await fetch(url)
const json = await response.json()
debugger
return json
注意:正如Thomas在一篇评论中指出的那样——如果您只是在异步函数中返回response.json,则不应将其展开——它将被包装在另一个承诺中。只有当您进入调试器并尝试检查它时,才需要从承诺中获取值。实际上,您的函数应该更像这样:
getCustomerById: async (id) => {
const url = 'http://example.com/customer/' + id
return fetch(url).then((response) => response.json())
}
你必须明白的是,一旦你步入未来,我保证这最终会返回一个值,你将永远不会回到同步一步一步处理的无聊和安全的世界
// It does not matter what you do inside this function or
// what you return. Because of the await keyword, this will
// **always** return a promise.
//
getCustomerById: async (id) => { /* ... */ }
承诺是一个容器,你可以随意传递这个容器。但是要查看容器内部,需要使用回调
或者使用async/await,这只是对.then的语法糖。然后对你来说,剩下的代码作为回调留在异步函数中
所以要得到结果,你需要等待承诺的解决。这就给你留下了两个选择:。然后,或者等待
我认为这对您的问题没有帮助,但是将响应声明为常量似乎很奇怪,您可以在希望获取数据的位置共享代码吗?这就是问题所在我无法访问该代码我无法访问该代码您能解释一下吗?您的方法getCustomerById是如何调用/使用的?当然,getCustomerById会返回一个承诺-这是一个异步函数!有什么问题吗?没关系。异步函数中的return-await-somePromise和return-somePromise一样没有意义。thenvalue=>value@Thomas
const apiKey = 'some_crazy_long_string_you_got_from_somewhere_safe'
const baseUrl = 'http://www.ready.buzlin.com/buzlinApp/customer'
// Let's show the object this is attached to, so we can
// actually make calls to the function.
//
const customerQueries = {
getCustomerById: async (id) => {
const url = `${baseUrl}/getCustomerById.php?k=${apiKey}&id=${id}`
let response
try {
response = await fetch(url)
return response.json()
} catch (err) {
// ... Properly handle error
// If you are not going to properly handle this error, then
// don't catch it. Just let it bubble up.
}
}
}
// Or a cleaner looking alternative that is also more true to the
// underlying behavior by skipping all this `async/await` nonsense.
//
const customerQueries2 = {
getCustomerById: (id) => {
const url = `${baseUrl}/getCustomerById.php?k=${apiKey}&id=${id}`
return fetch(url)
.then(response => response.json())
.catch(err => {
// ... Properly handle error
})
}
// Now to get at the result
//
// Using .then
customerQueries.getCustomerById(27)
.then(customer => {
//
// ... Do something with customer
//
})
// Or using async/await
(async () => {
const customer = await customerQueries.getCustomerById(27)
//
// ... Do something with customer
//
})()