📌  相关文章
📜  什么是异步函数 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:13.528000             🧑  作者: Mango

代码示例6
// Old School Javascript Invoke
(async function() {
    await someAsyncFunction();
})();

//New ES6 Javascript Invoke
(async () => {
    await someAsyncFunction();
})();

//Example (async & await)
function delayResult() {
 return new Promise(resolve => {
   setTimeout(() => {
     resolve(‘Done’);
   }, 5000)
 })
}
async function getResult() {
 let result = await delayResult();
 return result;
}
getResult();

//New Example 
const data = async ()  => {
  const got = await fetch('https://jsonplaceholder.typicode.com/todos/1');
  
  console.log(await got.json())
}

data();