async
async 是 ES7 才有的与异步操作有关的关键字,和 Promise , Generator 有很大关联的。
语法
async function name([param[, param[, ... param]]]) { statements } - name: 函数名称。
- param: 要传递给函数的参数的名称。
- statements: 函数体语句。
返回值
async 函数返回一个 Promise 对象,可以使用 then 方法添加回调函数。
async function helloAsync(){ return "helloAsync"; } console.log(helloAsync()) // Promise {<resolved>: "helloAsync"} helloAsync().then(v=>{ console.log(v); // helloAsync })
async 函数中可能会有 await 表达式,async 函数执行时,如果遇到 await 就会先暂停执行 ,等到触发的异步操作完成后,恢复 async 函数的执行并返回解析值。
await 关键字仅在 async function 中有效。如果在 async function 函数体外使用 await ,你只会得到一个语法错误。
function testAwait(){ return new Promise((resolve) => { setTimeout(function(){ console.log("testAwait"); resolve(); }, 1000); }); } async function helloAsync(){ await testAwait(); console.log("helloAsync"); } helloAsync(); // testAwait // helloAsync
await
await 操作符用于等待一个 Promise 对象, 它只能在异步函数 async function 内部使用。
语法
[return_value] = await expression;
- expression: 一个 Promise 对象或者任何要等待的值。
返回值
返回 Promise 对象的处理结果。如果等待的不是 Promise 对象,则返回该值本身。
如果一个 Promise 被传递给一个 await 操作符,await 将等待 Promise 正常处理完成并返回其处理结果。
function testAwait (x) { return new Promise(resolve => { setTimeout(() => { resolve(x); }, 2000); }); } async function helloAsync() { var x = await testAwait ("hello world"); console.log(x); } helloAsync (); // hello world
正常情况下,await 命令后面是一个 Promise 对象,它也可以跟其他值,如字符串,布尔值,数值以及普通函数。
function testAwait(){ console.log("testAwait"); } async function helloAsync(){ await testAwait(); console.log("helloAsync"); } helloAsync(); // testAwait // helloAsync
await针对所跟不同表达式的处理方式:
- Promise 对象:await 会暂停执行,等待 Promise 对象 resolve,然后恢复 async 函数的执行并返回解析值。
- 非 Promise 对象:直接返回对应的值。
漫天绯羽
176***3519@qq.com
async function a(){ console.log("1") console.log("2") } a() console.log("3") //打印: 1 2 3使用 await 测试:
async function a(){ await 1 console.log("1") console.log("2") } a() console.log("3") //打印: 3 1 2漫天绯羽
176***3519@qq.com
Kandy
474***32@qq.com
function getApiData() { // Promise异步编程的一种解决方案 // 第一个参数是 Promise 执行成功时的回调,第二个参数是 Promise 执行失败时的回调 return new Promise((resolve, reject) => { // 模拟延迟(获取服务的数据) setTimeout(() => { // 执行成功 resolve({name: 'kandy', age: 99}) }, 3000) }) } let user = {} async function getUser() { // 执行 await getApiData().then(res => { user = res }) // await后的该代码块在此会阻塞,直到 getApiData 执行后 then 完成工作后才能继续往下执行 console.log('获取数据完成: ', user) } getUser() // 异步执行的方法 console.log('开始获取数据:') // 此处无阻塞(同步代码)结果:
3 秒后:
获取数据完成: {name: 'kandy', age: 99}Kandy
474***32@qq.com