[英]Async/Await not waiting for response, and returns Promise object
Using ES6, Classes, and Aync/Await... 使用ES6,类和Aync / Await ...
The goal is to have an "Api" class that does async calls with fetch, and returns some data.... but even the basic foundation isn't working. 目的是要有一个“ Api”类,该类通过提取进行异步调用,并返回一些数据。。。。
in the main js these snippet runs, which starts the chain of events: 在主要的js中,这些代码段会运行,从而启动事件链:
let api = new Api();
let api_data = api.getLocation();
console.log(api_data);
getLocation method is the following, which would return some response/data. getLocation方法如下,它将返回一些响应/数据。 However, that data would theoretically be a fetch call to an API, which is "getTestVariable" for example that waits some time... 但是,从理论上讲,该数据将是对API的提取调用,例如,它是“ getTestVariable”,需要等待一段时间...
class Api {
getLocation = async () => {
var response = await this.getTestVariale();
console.log(response);
return response;
}
getTestVariale = () =>{
setTimeout(function(){
console.log("timeout done...");
return "this is the test variable";
},2000);
}
}
However, 1) the console.log(response) gives "undefined" because its not awaiting... and 2) back in the main js, api_data when logged, is some Promise object rather than the variable response 但是,1)console.log(response)给出了“ undefined”,因为它没有等待... 2)回到主js api_data时,它是一些Promise对象而不是变量response
As the comment above states, setTimeout
does not return a Promise, so getTestVariable
has no return value. 如上面的注释所述, setTimeout
不返回Promise,因此getTestVariable
没有返回值。
Here's a slightly modified version of your code that will hopefully put you on the right track: 这是您的代码的略微修改版本,有望将您带入正确的轨道:
class Api {
getLocation = async () => {
var response = await this.getTestVariale();
console.log(response);
return response;
}
getTestVariale = () => {
return new Promise((resolve, reject) => {
if (thereIsError)
reject(Error('Error message'));
else
resolve({foo: 'bar'});
}
}
}
Drop a comment if I need to explain further I'd be happy to. 如果需要进一步解释,请发表评论。
In getLocation
you await
for a value that will come from this.getTestVariable
. 在getLocation
你await
的将来自值this.getTestVariable
。 In order for this to work this.getTestVariable
must return a Promise; 为了使此工作正常, this.getTestVariable
必须返回一个Promise; it can be done in two ways - making getTestVariable
an async
function or explicitly returning a Promise. 它可以通过两种方式完成-使getTestVariable
成为async
函数或显式返回Promise。
Since you're using setTimeout
(which is not an async
function) you're bound to use Promise. 由于您正在使用setTimeout
(这不是async
函数),因此您必然会使用Promise。 Here you go: 干得好:
class Api {
async getLocation() {
return await this.getTestVariable();
}
getTestVariable() {
return new Promise((res, rej) => {
setTimeout(() => res('test'), 2000)
});
}
}
async function main() {
let api = new Api;
console.log('Trying to get the location...');
console.log('Voila, here it is: ', await api.getLocation());
}
main();
Looks quite ugly but there's no way you can achieve it if you use set timeout. 看起来很丑陋,但如果使用set timeout,则无法实现。
The main point is in resolution of getTestVariable
with value you want it to return. 要点是要解析getTestVariable
以及要返回的值。
Quite an important remark: you can mark getTestVariable
as an async
function, it will ad an extra Promise level, but you still will have the desired result. 非常重要的一句话:您可以将getTestVariable
标记为一个async
函数,它将附加一个Promise级别,但是您仍将获得所需的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.