[英]How does Async/await work and why does it not work here
I've been trying to understand how async/await works, all I want to do is have it wait until the value is returned. 我一直在尝试了解异步/等待的工作方式,我要做的就是让它等待直到返回值。 However, I could not get it to do so with callbacks nor promises nor
async/await
. 但是,我无法通过回调,promise或
async/await
做到这一点。 What am I doing wrong, and why does async/await
not work as I expect it to? 我在做什么错,为什么
async/await
无法正常工作? (wait with running other code until this promise is resolved) (等待运行其他代码,直到此承诺得到解决)
Many questions like this one link to "introduction pages". 像这样的许多问题都链接到“介绍页面”。 I've read them all, I understand what they do, I just don't know how to properly write them out, as I believe I did everything correctly, I'm just missing something
console.log("xcdcxcxcxccxvccvffdgfcd")
; 我已经阅读了所有内容,我了解它们的工作方式,我只是不知道如何正确地将它们写出,因为我相信我所做的一切正确,我只是缺少了一些
console.log("xcdcxcxcxccxvccvffdgfcd")
;
thing(); async function thing() { let c = await otherthing(); console.log("dfasfadsfdasasdfa" + c) } async function otherthing() { await setTimeout(function() { return new Promise((resolve, reject) => { let f = "rerewwqfewfasgsadf" return resolve(f); }) }, 3000); }
console.log
is supposed to wait until the promised value c
is returned, however, it does not seem to work. 应该将
console.log
等待到返回承诺值c
为止,但是,它似乎不起作用。 Why? 为什么?
Async/await works with functions that return promises. 异步/等待与返回承诺的函数一起使用。 Your
otherthing
function doesn't return anything. 您的
otherthing
功能不返回任何内容。
You can fix the code by returning the promise you are instantiating, like this: 您可以通过返回实例化的承诺来修复代码,如下所示:
thing(); async function thing() { let c = await otherthing(); console.log("dfasfadsfdasasdfa" + c) } function otherthing() { return new Promise((resolve, reject) => { setTimeout(function () { let f = "rerewwqfewfasgsadf" resolve(f); }, 3000) }); }
You must return
the new Promise
from the otherthing
function, not from the setTimeout
callback. 您必须从
otherthing
功能(而不是从setTimeout
回调) return
new Promise
。 You are supposed to construct the promise, in its executor callback start the asynchronous action ( setTimeout
) and then asynchronously resolve()
or reject()
the promise. 您应该构造诺言,在其执行器回调中启动异步操作(
setTimeout
),然后异步实现诺言resolve()
或reject()
。
function otherthing() {
return new Promise((resolve, reject) => {
setTimeout(function(){
let f = "rerewwqfewfasgsadf"
resolve(f);
}, 3000);
});
}
You don't need any async
/ await
here for the function that is essentially only a promise wrapper around setTimeout
. 您不需要
await
这里进行任何async
/ await
,该函数本质上只是setTimeout
周围的一个Promise包装器。 See also How do I convert an existing callback API to promises? 另请参见如何将现有的回调API转换为Promise? .
。
You should move setTimeout
inside Promise
body and there do resolve(f)
您应该在
Promise
主体内移动setTimeout
然后执行resolve(f)
function otherthing() {
return new Promise((resolve, reject) => {
setTimeout(() => {
let f = "rerewwqfewfasgsadf"
resolve(f);
}, 3000);
});
}
async function otherthing
has no return
statement, so it will always return a promise that resolves to undefined
. async function otherthing
没有return
语句,因此它将始终返回解析为undefined
的promise。
await setTimeout(...)
doesn't make any sense. await setTimeout(...)
没有任何意义。 You can only await
a promise, and setTimeout
does not return a promise. 您只能
await
一个承诺,而setTimeout
不会返回一个承诺。
You need to explicitly: 您需要明确地:
new Promise()
inside otherthing
new Promise()
内otherthing
resolve()
it with the desired value inside the callback function you pass to setTimeout
setTimeout
的回调函数中的resolve()
具有所需的值 return
the promise from otherthing
return
从承诺otherthing
Don't create a promise inside the callback function you pass to setTimeout
. 不要在传递给
setTimeout
的回调函数中创建promise。 It is pointless there. 那里没有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.