JavaScript詳解使用Promise處理回調(diào)地獄的兩種方法
一、.then()鏈?zhǔn)秸{(diào)用解決
多層回調(diào)函數(shù)的相互嵌套,就形成了回調(diào)地獄。
缺點(diǎn):
1.代碼耦合性太強(qiáng),難以維護(hù)。
2.大量冗余的代碼相互嵌套,可讀性比較差。
then-fs的基本使用:
調(diào)用then-fs(終端通過(guò)npm install then-fs安裝包)提供的readFile()方法,可以異步的讀取文件的內(nèi)容,它的返回值是Promise的實(shí)例對(duì)象,因此可以調(diào)用.then()方法為每個(gè)Promise異步操作指定成功和失敗之后的回調(diào)函數(shù)。
import thenFs from "then-fs"
thenFs.readFile("./fis/1.txt","utf8").then(r1=>{
console.log(r1);
})
thenFs.readFile("./fis/2.txt","utf8").then(r2=>{
console.log(r2);
})
thenFs.readFile("./fis/3.txt","utf8").then(r3=>{
console.log(r3);
})
但是會(huì)發(fā)現(xiàn)讀取順序是會(huì)變化的。
.then()方法的特性:
如果上一個(gè).then()方法中返回了一個(gè)新的promise實(shí)例對(duì)象,則可以通過(guò)下一個(gè).then()繼續(xù)進(jìn)行處理,通過(guò)鏈?zhǔn)秸{(diào)用的方法,解決地獄回調(diào)問(wèn)題。
import thenFs from "then-fs"
thenFs.readFile("./fis/1.txt","utf8").then(r1=>{
console.log(r1);
return thenFs.readFile("./fis/2.txt","utf8")
}).then(r2=>{
console.log(r2);
return thenFs.readFile("./fis/3.txt","utf8")
}).then(r3=>{
console.log(r3);
})
.catch()方法
import thenFs from "then-fs"
thenFs.readFile("./fis/11.txt","utf8").catch(err=>{
console.log(err.message);
}).then(r1=>{
console.log(r1);
return thenFs.readFile("./fis/2.txt","utf8")
}).then(r2=>{
console.log(r2);
return thenFs.readFile("./fis/3.txt","utf8")
}).then(r3=>{
console.log(r3);
})
.catch()放到最后可以捕獲所有錯(cuò)誤,但是一旦遇到錯(cuò)誤,后面的.then()就不在執(zhí)行。
.catch()放到前面,后面的.then()還可以正常執(zhí)行。
二、async await解決
import thenFs from "then-fs"
async function getFile(){
// 不加await,打印的r1是一個(gè)promise實(shí)例對(duì)象,加await打印的是結(jié)果
const r1=await thenFs.readFile("./fis/1.txt","utf8")
console.log(r1);
const r2=await thenFs.readFile("./fis/2.txt","utf8")
console.log(r2);
const r3=await thenFs.readFile("./fis/3.txt","utf8")
console.log(r3);
}
getFile()注意:
在async方法中,第一個(gè)await之前的代碼會(huì)同步執(zhí)行,await之后的代碼會(huì)異步執(zhí)行
import thenFs from "then-fs"
// 在async方法中,第一個(gè)await之前的代碼會(huì)同步執(zhí)行,await之后的代碼會(huì)異步執(zhí)行
console.log("a");
async function getFile(){
console.log("b");
const r1=await thenFs.readFile("./fis/1.txt","utf8")
console.log(r1);
const r2=await thenFs.readFile("./fis/2.txt","utf8")
console.log(r2);
const r3=await thenFs.readFile("./fis/3.txt","utf8")
console.log(r3);
console.log("d");
}
getFile()
console.log("c");
到此這篇關(guān)于JavaScript詳解使用Promise處理回調(diào)地獄的兩種方法的文章就介紹到這了,更多相關(guān)JS Promise回調(diào)地獄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript省市區(qū)三級(jí)聯(lián)動(dòng)菜單效果
這篇文章主要為大家詳細(xì)介紹了JavaScript省市區(qū)三級(jí)聯(lián)動(dòng)菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
JS使用canvas實(shí)現(xiàn)基本的截圖功能
這篇文章主要給大家介紹了使用JS中的canvas實(shí)現(xiàn)基本的截圖功能,文中有詳細(xì)的實(shí)現(xiàn)思路和實(shí)現(xiàn)過(guò)程,文章通過(guò)代碼示例講解的非常詳細(xì),很感興趣的同學(xué)可以參考一下2023-08-08
es6函數(shù)之嚴(yán)格模式用法實(shí)例分析
這篇文章主要介紹了es6函數(shù)之嚴(yán)格模式用法,結(jié)合實(shí)例形式分析了es6函數(shù)嚴(yán)格模式的定義、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
簡(jiǎn)單使用webpack打包文件的實(shí)現(xiàn)
這篇文章主要介紹了簡(jiǎn)單使用webpack打包文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
javascript中return,return true,return false三者的用法及區(qū)別
這篇文章主要介紹了javascript中return,return true,return false三者的用法及區(qū)別的相關(guān)資料,需要的朋友可以參考下2015-11-11
詳解全棧開(kāi)發(fā)Vercel數(shù)據(jù)庫(kù)存儲(chǔ)服務(wù)
這篇文章主要為大家介紹了全棧開(kāi)發(fā)Vercel數(shù)據(jù)庫(kù)存儲(chǔ)服務(wù)功能使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

