#author("2022-05-13T13:41:14+08:00","default:Admin","Admin")
#author("2022-07-29T11:17:47+08:00","default:Admin","Admin")
[[Vue]]

&color(red){※前提条件:本文基于 Vue 2.0 创作};
#contents

* 概要 [#nd5552ca]

用async/await 来处理异步

语法很简单,就是在函数前面加上async,他就成异步函数啦。怎么去调用呢,其实一样,平时怎么使用函数我们就怎么去调用它,直接加括号就可以。

#codeprettify{{
async function hello(){
    return 'hello world';
}
}}

* 错误 [#n93dbe2f]

** await is a reserved word [#q5bf4ae7]

#codeprettify{{

function funcA(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}

//正确应该在 forEach 里面设置 async
function succ(){
	let arr =["a","b"];
	arr.forEach(async val=>{
		await funcA(2000);
		console.log(val);
	})
}
}}




#hr();
コメント:
#comment_kcaptcha

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS