ES6中async函數(shù)與await表達(dá)式的基本用法舉例
一、async 函數(shù)
概念:
async 是一個(gè)修飾符,async 定義的函數(shù)會默認(rèn)的返回一個(gè)Promise對象resolve(成功值)的值,因此對async函數(shù)可以直接進(jìn)行then操作,返回的值即為then方法的傳入函數(shù)。
舉例:
async function demo(){
// 1:當(dāng)返回值不是promise對象 當(dāng)調(diào)用函數(shù)的時(shí)候就是已成功的值
// return "succ";
// 2:當(dāng)返回的是promise對象 那么函數(shù)(promise對象)的結(jié)果與返回的promise狀態(tài)一致
return new Promise((resolve,reject)=>{ //Promise 內(nèi)容請參考上期作品,關(guān)注專欄。
let flag = true;
if(flag){
resolve("succ");
}else{
reject("error");
}
})
}
const MyPromise = demo();
MyPromise.then((resolve)=>{
console.log(resolve);
},(reject)=>{
console.log(reject);
})二、await表達(dá)式
它也是一個(gè)修飾符,await 關(guān)鍵字 只能放在 async 函數(shù)內(nèi)部, await關(guān)鍵字的作用 就是獲取 Promise中返回的內(nèi)容, 獲取的是Promise函數(shù)中resolve。
1.await必須放在async函數(shù)中
2.await右側(cè)的表達(dá)式一般為promise對象
3.await可以返回的是右側(cè)promise成功的值
4.await右側(cè)的promise如果失敗了,就會拋出異常,需要通過try…catch捕獲處理
舉例:
// 1:await需要寫在async函數(shù)的內(nèi)部
// 2:await 修飾的Promise 返回的值就是resolve的值
// 3:后面的代碼需要等待 await后的結(jié)果
async function demo(){
const a = await "a";
const b = await new Promise((resolve,reject)=>{
setTimeout(()=>{
console.log("定時(shí)器執(zhí)行了....");
resolve("b");
},3000);
});
const c = await "c";
console.log(a,b,c);
}
demo();舉例:失敗的代碼 await 錯(cuò)誤的代碼 需要用try catch捕獲
async function demo(){
try{
const a = await new Promise((relsolve,reject)=>{
reject("數(shù)據(jù)不存在");
})
}catch(error){
console.log(error);
}
}
demo();
三、async await ajax 基礎(chǔ)使用
function mark (url){
return new Promise((resolve,reject)=>{
const ajax = new XMLHttpRequest();
ajax.open("GET",url)
ajax.send();
ajax.onreadystatechange=function(){
if(ajax.readyState==4){
if(ajax.status==200){
resolve(JSON.parse(ajax.response));
}
}
}
})
}
async function demo(){
const res = await mark("http://127.0.0.1:5500/test.json")補(bǔ)充:async await ajax使用
首先要?jiǎng)?chuàng)建對象,用get的方法請求后面?zhèn)魅氲牡刂罚侔l(fā)送請求,通過判斷出輸出有無數(shù)據(jù)。
function sendajax(url){
return new Promise((resolve,reject)=>{
const http = new XMLHttpRequest();//創(chuàng)建對象
http.open("GET",url);//用get方法請求地址
http.send();//發(fā)送請求
http.onreadystatechange = function(){
if(http.readyState==4){
if(http.status==200){
resolve(JSON.parse(http.response));
}
}
}
})
}
async function demo(){
const res = await sendajax("http://127.0.0.1:8848/web2209/ES6/test.json");
if(res.code==200){
console.log("有數(shù)據(jù)");
}else{
console.log("無數(shù)據(jù)");
}
}
demo();
總結(jié)
到此這篇關(guān)于ES6中async函數(shù)與await表達(dá)式的基本用法的文章就介紹到這了,更多相關(guān)ES6 async函數(shù)與await表達(dá)式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解ES6數(shù)組方法find()、findIndex()的總結(jié)
這篇文章主要介紹了詳解ES6數(shù)組方法find()、findIndex()的總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
layui 對table中的數(shù)據(jù)進(jìn)行轉(zhuǎn)義的實(shí)例
今天小編就為大家分享一篇layui 對table中的數(shù)據(jù)進(jìn)行轉(zhuǎn)義的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
JavaScript設(shè)計(jì)模式之職責(zé)鏈模式詳解
職責(zé)鏈模式的定義是:使多個(gè)對象都有機(jī)會處理請求,從而避免請求的發(fā)送者和接收者之間的耦合關(guān)系,將這些對象連成一條鏈,并沿著這條鏈傳遞該請求,直到有一個(gè)對象處理它為止2022-08-08
JavaScript實(shí)現(xiàn)復(fù)選框全選功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)復(fù)選框全選功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
bootstrap datepicker限定可選時(shí)間范圍實(shí)現(xiàn)方法
這篇文章主要介紹了bootstrap datepicker限定可選時(shí)間范圍的實(shí)現(xiàn)方法,本文涉及到相關(guān)知識點(diǎn),通過實(shí)例給大家介紹的非常詳細(xì),需要的朋友可以參考下2016-09-09

