Promise
含義:
Promise 是異步編程的一種解決方案,比傳統的解決方案——回呼函式和事件——更合理和更強大,它由社區最早提出和實作,ES6 將其寫進了語言標準,統一了用法,原生提供了Promise物件,
實體化Promise
const p = new Promise();
接收一個引數,這個引數是一個函式,函式有兩個引數resolve和reject,異步函式過后,p.then(),里面有兩個回呼函式,前面那個是成功后的,后面一個是失敗時的,如果異步函式中呼叫resolve則呼叫第一個函式,否則呼叫第二個,
const p =new Promise(function (resolve,reject) { setTimeout(function () { // let data ='https://www.cnblogs.com/meteorll/archive/2020/10/27/resolve成功'; // resolve(data); let err='reject失敗' reject(err) },1000) }); p.then(function (value) { console.log(value); },function (reason) { console.error(reason); })
讀取檔案
const fs=require('fs');
?
const p=new Promise(function(resolve,reject){
?
fs.readFile("./chunxiao.mdd",(err,data)=>{
if(err){
reject(err)
}else(
resolve(data)
)
})
})
p.then(function(value){
console.log(value.toString());
},function(reason){
console.log('讀取失敗');
?
})
Ajax函式封裝
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const xhr= new XMLHttpRequest();
const p=new Promise((resolve,reject)=>{
xhr.open('get','https://api.apiopen.top/getJoke');
xhr.send();
xhr.onreadystatechange=function(){
if(xhr.readyState ===4){
if(xhr.status>=200&xhr.status<300){
resolve(xhr.response);
?
}else{
reject(xhr.status);
}
}
}
})
p.then(function(value){
console.log(value)
},function (reason) {
console.log(reason)
})
</script>
</body>
</html>
初試catch
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const p =new Promise(function (resolve,reject) {
setTimeout(function () {
// let data ='https://www.cnblogs.com/meteorll/archive/2020/10/27/resolve成功';
// resolve(data);
let err='catch失敗'
reject(err)
},1000)
});
//
p.catch(function (reason) {
console.warn(reason);
})
</script>
</body>
</html>
catch總結:其實可以把catch看作是then的語法糖,里面就少了一個resolve的引數,
Promise.prototype.finally()
finally()方法用于指定不管 Promise 物件最后狀態如何,都會執行的操作,該方法是 ES2018 引入標準的,
promise .then(result => {···}) .catch(error => {···}) .finally(() => {···});
上面代碼中,不管promise最后的狀態,在執行完then或catch指定的回呼函式以后,都會執行finally方法指定的回呼函式,
實體(體現promise的好處)
通過新建一個 Promise 物件好像并沒有看出它怎樣 "更加優雅地書寫復雜的異步任務",我們之前遇到的異步任務都是一次異步,如果需要多次呼叫異步函式呢?例如,如果我想分三次輸出字串,第一次間隔 1 秒,第二次間隔 4 秒,第三次間隔 3 秒:
setTimeout(function () { console.log("First"); setTimeout(function () { console.log("Second"); setTimeout(function () { console.log("Third"); }, 3000); }, 4000); }, 1000);
這段程式實作了這個功能,但是它是用 "函式瀑布" 來實作的,可想而知,在一個復雜的程式當中,用 "函式瀑布" 實作的程式無論是維護還是例外處理都是一件特別繁瑣的事情,而且會讓縮進格式變得非常冗贅,
現在我們用 Promise 來實作同樣的功能:
new Promise(function (resolve, reject) { setTimeout(function () { console.log("First"); resolve(); }, 1000); }).then(function () { return new Promise(function (resolve, reject) { setTimeout(function () { console.log("Second"); resolve(); }, 4000); }); }).then(function () { setTimeout(function () { console.log("Third"); }, 3000); });
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/195081.html
標籤:其他
