我研究了很多帖子、文章和文档。我正在寻求更深入的理解。
来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
该
await表达式导致async函数执行暂停,直到 aPromise确定,[...]
let fetchLocation = () => {
//fetches information from satellite system and
//returns a Vector2 of lat long on earth
return new Promise(resolve => {
setTimeout(() => {
resolve({
lat: 73,
long: -24
});
}, 2000);
});
}
//async functions automatically return a promise.
let main = async() => {
//awaits the promises resolution
let result = await fetchLocation();
console.log("I'm the awaited promise result", result)
//immediately returns a promise …Run Code Online (Sandbox Code Playgroud)