<分区>
我注意到 async
关键字可以被赋予任何值,甚至可以用作普通变量:
let async = "world";
console.log(async)
console.log("Hello " + async)
然而,即便如此,它仍然像以前一样运行:
let async = "world";
async function foo(input) {
return input;
}
let barPromise = foo("bar");
console.log("bar promise is:", typeof barPromise);
console.log("bar promise also has a .then:", typeof barPromise.then === "function");
barPromise
.then(output => console.log("bar promise returns:", output));
console.log("async is still assigned:", async);
foo("Hello ")
.then(output => console.log(output + async));
甚至 await
也在做同样的事情:
let await = "world";
console.log("await is:", await)
async function foo(input) {
return input;
}
async function bar(input) {
console.log("before");
let output = await foo(input);
console.log(output);
console.log("after");
}
bar("hello")
这是怎么回事?为什么关键字可以用作变量?