[英]javascript async/await and promise
I'm having a hard time understanding how async/await works.I have to make a program which contains three functions: func1
func2
and concatenated
. 我很难理解async / await是如何工作的。我必须创建一个包含三个函数的程序:
func1
func2
和concatenated
。 func1
takes a string as an argument and returns the same string after 5 seconds of delay, func2
is an async
function which also takes a string as an argument and returns the same string. func1
将一个字符串作为参数,并在延迟5秒后返回相同的字符串, func2
是一个async
函数,它也将一个字符串作为参数并返回相同的字符串。 concatenated
is a function that takes two strings (s1,s2)
as arguments and uses the above two functions( (func1(s1) and func2(s2))
) to return their concatenated result after 5 seconds. concatenated
是一个函数,它接受两个字符串(s1,s2)
作为参数,并使用上述两个函数( (func1(s1) and func2(s2))
)在5秒后返回它们的连接结果。 So if we pass ("hello"," world")
to concatenated
it should return hello world
. 因此,如果我们传递
("hello"," world")
concatenated
它应该返回hello world
。 My code is: 我的代码是:
function func1(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 5000);
});
}
async function func2(x) {
const a = await func1(x);
return a;
}
function concatenated(a,b){
const c = func2(a).then(result =>{console.log(result)});
const d = func2(b).then(result =>{console.log(result)});
return (c+d) ;
}
concatenated("hello"," world")
This code only gives me: 这段代码只给了我:
hello world
How can I correct this? 我怎么能纠正这个?
The problem is that your return statement from the concatenated
function will be run synchronously . 问题是来自
concatenated
函数的return语句将同步运行。 This also means that c
and d
will still be promises. 这也意味着
c
和d
仍将是承诺。
A possible solution would be: 一个可能的解决方案是:
async function concatenated(a,b){
const c = await func2(a);
const d = await func2(b);
return (c+d);
}
concatenated("hello", " world").then(result => {
console.log(result); // hello world
})
Notice that an async function will always return a promise. 请注意, 异步函数将始终返回promise。
You can get the result after 5 seconds like this: 你可以在5秒后得到这样的结果:
function func1(x) { return new Promise(resolve => { setTimeout(() => { resolve(x); }, 5000); }); } async function func2(x) { const a = await func1(x); return a; } async function concatenated(a,b){ const [c,d] = await Promise.all([func2(a), func2(b)]) return c+d; } (async function main() { const ret = await concatenated("hello"," world") console.log(ret) })()
Using JavaScript async/await
syntax, you can write the asynchronous code like the synchronous style. 使用JavaScript
async/await
语法,您可以像同步样式一样编写异步代码。 No need promise.then
, no need callback. 不需要
promise.then
,不需要回电。 It's a little different from other languages, eg Go, Java 它与其他语言略有不同,例如Go,Java
You seem to be misunderstanding console logs. 你似乎误解了控制台日志。 A regular
console.log
will always put a newline, which is why you are seeing hello world on two lines rather than one. 常规的
console.log
将始终使用换行符,这就是为什么您在两行而不是一行上看到hello world的原因。 Assuming you are using Node.js, you can use the following to write to the console without a newline to achieve your desired outcome: 假设您正在使用Node.js,您可以使用以下内容写入控制台而不使用换行符来实现您期望的结果:
process.stdout.write(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.