javascript中Promise的三種狀態(tài)示例詳解
說明
通過了解Promise的三種狀態(tài),可以了解Promise對象是如何關(guān)聯(lián)的處理函數(shù),以及代碼的執(zhí)行順序。
一個Promise對象,必然處于以下幾種狀態(tài)之一:
- pending(待定):初始狀態(tài),既沒有被兌現(xiàn),也沒有被拒絕
- fullfilled(已兌現(xiàn)):操作成功完成
- rejected(已拒絕):操作失敗
Promise對象一旦被兌現(xiàn)或拒絕了,就是已敲定了,狀態(tài)無法再被改變。
示例
Promise創(chuàng)建后處于pending狀態(tài)
注意:下面代碼中,為了有時間查看狀態(tài),所以setTimeout的超時時間可以設(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>認識Promise狀態(tài)</title> </head> <body> <script> /** * 目標:認識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>認識Promise狀態(tài)</title> </head> <body> <script> /** * 目標:認識Promise狀態(tài) */ // 1. 創(chuàng)建Promise對象 const p = new Promise((resolve, reject) => { // Promise對象創(chuàng)建時,這里面的代碼都會被執(zhí)行了,然后then()內(nèi)的回調(diào)函數(shù)執(zhí)行 console.log('Promise對象內(nèi)開始執(zhí)行') // 2. 執(zhí)行異步代碼 setTimeout(() => { // 當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è)置的是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>認識Promise狀態(tài)</title> </head> <body> <script> /** * 目標:認識Promise狀態(tài) */ // 1. 創(chuàng)建Promise對象 const p = new Promise((resolve, reject) => { // Promise對象創(chuàng)建時,這里面的代碼都會被執(zhí)行了 console.log('Promise對象內(nèi)開始執(zhí)行') // 2. 執(zhí)行異步代碼 setTimeout(() => { // 當resolve被調(diào)用后,Promise狀態(tài)就變成了fullfilled狀態(tài),然后then()內(nèi)的回調(diào)函數(shù)執(zhí)行 // resolve('模擬AJAX請求-成功結(jié)果') // 當 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ù)都打開,一個執(zhí)行以后,狀態(tài)就不會再改了
5秒鐘以后:
總結(jié)
到此這篇關(guān)于javascript中Promise的三種狀態(tài)的文章就介紹到這了,更多相關(guān)javascript Promise狀態(tài)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript操作XML實例代碼(獲取新聞標題并分頁,并分頁)
XML 代碼部分 這是一個新聞的XML 文件,如果 NBody部分包含 XML 和Html 不可識別部分, 就 包含在DATA 表示附中。2010-05-05JavaScript 判斷指定字符串是否為有效數(shù)字
最近在做一個ColdFusion的項目,有一個業(yè)務(wù)Check,需要用JavaScript實現(xiàn):判斷指定字符串是否為有效數(shù)字。2010-05-05javascript與css3動畫結(jié)合使用小結(jié)
本文給大家講述的是如何使用javascript結(jié)合CSS動畫來實現(xiàn)一些占用資源更少,更優(yōu)化的動畫效果,思路十分巧妙,這里推薦給小伙伴們參考下。2015-03-03微信小程序使用checkbox顯示多項選擇框功能【附源碼下載】
這篇文章主要介紹了微信小程序使用checkbox顯示多項選擇框功能,涉及相關(guān)事件綁定與元素遍歷操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2017-12-12