javascript中Promise的三種狀態(tài)示例詳解
說明
通過了解Promise的三種狀態(tài),可以了解Promise對象是如何關(guān)聯(lián)的處理函數(shù),以及代碼的執(zhí)行順序。
一個(gè)Promise對象,必然處于以下幾種狀態(tài)之一:
- pending(待定):初始狀態(tài),既沒有被兌現(xiàn),也沒有被拒絕
- fullfilled(已兌現(xiàn)):操作成功完成
- rejected(已拒絕):操作失敗
Promise對象一旦被兌現(xiàn)或拒絕了,就是已敲定了,狀態(tài)無法再被改變。

示例
Promise創(chuàng)建后處于pending狀態(tài)
注意:下面代碼中,為了有時(shí)間查看狀態(tài),所以setTimeout的超時(shí)時(shí)間可以設(shè)置得大一些,如果設(shè)置成1秒、2秒,可能來不及看,狀態(tài)就改變了。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>認(rèn)識(shí)Promise狀態(tài)</title>
</head>
<body>
<script>
/**
* 目標(biāo):認(rèn)識(shí)Promise狀態(tài)
*/
// 1. 創(chuàng)建Promise對象
const p = new Promise((resolve, reject) => {
// 2. 執(zhí)行異步代碼
setTimeout(() => {
// resolve('模擬AJAX請求-成功結(jié)果')
reject(new Error('模擬AJAX請求-失敗結(jié)果'))
}, 5000)
})
console.log(p)
// 3. 獲取結(jié)果
p.then(result => {
console.log(result)
}).catch(error => {
console.log(error)
})
</script>
</body>
</html>

通過打印日志查看Promise的成功狀態(tài)的改變順序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>認(rèn)識(shí)Promise狀態(tài)</title>
</head>
<body>
<script>
/**
* 目標(biāo):認(rèn)識(shí)Promise狀態(tài)
*/
// 1. 創(chuàng)建Promise對象
const p = new Promise((resolve, reject) => {
// Promise對象創(chuàng)建時(shí),這里面的代碼都會(huì)被執(zhí)行了,然后then()內(nèi)的回調(diào)函數(shù)執(zhí)行
console.log('Promise對象內(nèi)開始執(zhí)行')
// 2. 執(zhí)行異步代碼
setTimeout(() => {
// 當(dāng)resolve被調(diào)用后,Promise狀態(tài)就變成了fullfilled狀態(tài)
resolve('模擬AJAX請求-成功結(jié)果')
// reject(new Error('模擬AJAX請求-失敗結(jié)果'))
}, 5000)
})
console.log(p)
// 3. 獲取結(jié)果
p.then(result => {
console.log(result)
}).catch(error => {
console.log(error)
})
</script>
</body>
</html>

5秒(代碼中定時(shí)器設(shè)置的是5000毫秒)以后,狀態(tài)改變?yōu)閒ullfilled:

通過打印日志查看Promise的失敗狀態(tài)的改變順序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>認(rèn)識(shí)Promise狀態(tài)</title>
</head>
<body>
<script>
/**
* 目標(biāo):認(rèn)識(shí)Promise狀態(tài)
*/
// 1. 創(chuàng)建Promise對象
const p = new Promise((resolve, reject) => {
// Promise對象創(chuàng)建時(shí),這里面的代碼都會(huì)被執(zhí)行了
console.log('Promise對象內(nèi)開始執(zhí)行')
// 2. 執(zhí)行異步代碼
setTimeout(() => {
// 當(dāng)resolve被調(diào)用后,Promise狀態(tài)就變成了fullfilled狀態(tài),然后then()內(nèi)的回調(diào)函數(shù)執(zhí)行
// resolve('模擬AJAX請求-成功結(jié)果')
// 當(dāng) reject被調(diào)用后,Promise狀態(tài)就變成了rejected狀態(tài),然后catch()內(nèi)的回調(diào)函數(shù)執(zhí)行
reject(new Error('模擬AJAX請求-失敗結(jié)果'))
}, 5000)
})
console.log(p)
// 3. 獲取結(jié)果
p.then(result => {
console.log(result)
}).catch(error => {
console.log(error)
})
</script>
</body>
</html>

過了5秒以后:

resolve和reject函數(shù)都打開,一個(gè)執(zhí)行以后,狀態(tài)就不會(huì)再改了


5秒鐘以后:

總結(jié)
到此這篇關(guān)于javascript中Promise的三種狀態(tài)的文章就介紹到這了,更多相關(guān)javascript Promise狀態(tài)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS獲取DropDownList的value值與text值的示例代碼
本篇文章主要是對JS獲取DropDownList的value值與text值的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
JavaScript常用代碼書寫規(guī)范的超全面總結(jié)
這篇文章給大家全面總結(jié)了JavaScript常用代碼的書寫規(guī)范,分別利用推薦和不推薦的兩種示例代碼讓大家更能直接的了解書寫規(guī)范,其實(shí)關(guān)于javascript代碼規(guī)范我們應(yīng)該遵循古老的原則:“能做并不意味著應(yīng)該做”,好了,下面我們就來一起看看吧。2016-09-09
javascript 二進(jìn)制運(yùn)算技巧解析
javascript 中的二進(jìn)制運(yùn)算的一些技巧,曬出來和你們分享一下,希望可以幫助你們2012-11-11
javascript/jquery獲取地址欄url參數(shù)的方法
本篇文章主要是對javascript/jquery獲取地址欄url參數(shù)的方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-03-03

