1. Promise 之前
1.1 回調(diào)函數(shù)
回調(diào)函數(shù):把函數(shù)A當作參數(shù)傳遞給另一個函數(shù)B調(diào)用,那么A就是回調(diào)函數(shù)?!就扑]:JavaScript視頻教程】
一些例子
具名回調(diào)
function 你有幾只狗(fn){ fn('一只狗') }function 數(shù)狗(數(shù)量){ console.log(數(shù)量) } 你有幾只狗(數(shù)狗) // 一只狗
匿名回調(diào)
function 你有幾只狗(fn){ fn('一只狗') } 你有幾只狗(function(數(shù)量){console.log(數(shù)量) }) // 一只狗
常見的例子
jQuery中使用回調(diào)函數(shù),這里用的是匿名回調(diào)的方式
$("#btn").click(function(){ console.log('點到我了') })
1.2 回調(diào)地獄(回調(diào)缺點1)
回調(diào)地獄:指的是回調(diào)嵌套過多的情況,導致代碼很難被看懂。
let info = []function 你有幾只狗(fn){ fn('一只狗') }function 你有幾只貓(fn){ fn('一只貓') }function 知道了(數(shù)量,callback){ info.push(數(shù)量) console.log(info) if(callback){ callback() } }// 開始調(diào)用 如果比這再多幾層,就不容易看懂了你有幾只狗(function(狗數(shù)){ console.log(狗數(shù)) 知道了(狗數(shù), function(){ 你有幾只貓(function(貓數(shù)){ console.log(貓數(shù)) 知道了(貓數(shù)) }) }) })
1.3 不使用Promise,如何解決
利用具名函數(shù)代替匿名函數(shù)
let info = []function 你有幾只狗(fn){ fn('一只狗') }function 你有幾只貓(fn){ fn('一只貓') }function 知道了(數(shù)量,callback){ info.push(數(shù)量) console.log(info) if(callback){ callback() } }function 告訴你貓的個數(shù)(貓數(shù)){ console.log(貓數(shù)) 知道了(貓數(shù)) }function 繼續(xù)數(shù)(){ 你有幾只貓(告訴你貓的個數(shù)) }function 告訴你狗的個數(shù)(狗數(shù)){ console.log(狗數(shù)) 知道了(狗數(shù), 繼續(xù)數(shù)) } 你有幾只狗(告訴你狗的個數(shù)) // 好像也沒好到哪去。。。
1.4 回調(diào)方式各不相同,需要單獨記憶(回調(diào)缺點2)
readFile('C:\\1.txt',function (error, data) { // node.js 讀取文件方法中的回調(diào) if(error) { console.log('成功') console.log(data.toString()) } else { console.log('讀取文件失敗') } }) $.ajax({ // jQuery中ajax方法中的回調(diào) url:'/2.txt' success: function(response) { console.log('成功') }, error: function(){ console.log('失敗') } })
2. Promise 的目的
Promise 是異步編程的一種解決方案,比傳統(tǒng)的解決方案——回調(diào)函數(shù)和事件——更合理和更強大。它由社區(qū)最早提出和實現(xiàn),ES6 將其寫進了語言標準,統(tǒng)一了用法,原生提供了Promise對象。
3. Promise 的原理
3.1 實現(xiàn)原理
ES6 規(guī)定,Promise對象是一個構(gòu)造函數(shù),用來生成Promise實例。通過在函數(shù)內(nèi)部return 一個 Promise對象的實例,這樣就可以使用Promise的屬性和方法進行下一步操作了。
function 函數(shù)名(){ return new Promise(function(resolve, reject) { // ... some code if (/* 異步操作成功 */){ resolve(value); // 異步操作成功時調(diào)用,把結(jié)果作為參數(shù)傳遞出去 } else { reject(error); // 異步失敗時調(diào)用,把錯誤作為參數(shù)傳遞出去 } }) }
3.2 調(diào)用邏輯
S1和E1兩個都沒有報錯,執(zhí)行S2(resolve執(zhí)行,系統(tǒng)認為搞定了,沒報錯)
S1和E1任何一個有報錯,執(zhí)行E2(reject執(zhí)行,系統(tǒng)認為沒搞定,報錯了)
4. Promise 的使用
4.1 Promise 的屬性與方法
屬性
Promise.prototype 表示 Promise 構(gòu)造器的原型
方法
Promise.prototype.then()
返回一個 Promise 。它最多需要有兩個參數(shù):Promise 的成功和失敗情況的回調(diào)函數(shù)。
Promise.prototype.catch()
返回一個Promise,并且處理拒絕的情況。等價于Promise.prototype.then(undefined, onRejected)
Promise.prototype.finally()
finally() 方法返回一個Promise,在執(zhí)行then()和catch()后,都會執(zhí)行finally指定的回調(diào)函數(shù)。避免同樣的語句需要在then()和catch()中各寫一次的情況。
Promise.all(iterable)
返回一個 Promise 實例,iterable參數(shù)內(nèi)所有的 promise 都resolved后,才回調(diào)完成resolve。
Promise.race(iterable)
返回一個 promise ,并伴隨著 promise對象解決的返回值或拒絕的錯誤原因, 只要 iterable 中有一個 promise 對象”解決(resolve)”或”拒絕(reject)”。
Promise.resolve()
返回一個以給定值解析后的Promise對象。但如果這個值是個thenable(即帶有then方法),返回的promise會“跟隨”這個thenable的對象,采用它的最終狀態(tài)(指resolved/rejected/pending/settled);如果傳入的value本身就是promise對象,則該對象作為Promise.resolve方法的返回值返回;否則以該值為成功狀態(tài)返回promise對象。
Promise.reject()
返回一個帶有拒絕原因reason參數(shù)的Promise對象。
4.2 將回調(diào)地獄中的例子,改寫為Promise的形式
可以看到使用 Promise后,邏輯變得非常直觀
寫得更完整一些
Promise套Promise時,也就是Promise鏈的時候——注意信息的傳遞
一個失敗的例子,當我們使用Promise鏈的時候,如果每一步都需要上一步的數(shù)據(jù)時,就需要傳參,成功通過resolve傳參,失敗通過reject傳參,如果忘記傳參,就得不到想要的結(jié)果。
resolve把成功的數(shù)據(jù)返回給下一個回調(diào)
reject把失敗的數(shù)據(jù)返回給下一個回調(diào)。
給這里的resolve傳一個參
改成失敗的例子
先不給reject傳參,如果失敗的話,下一個回調(diào)拿不到數(shù)據(jù)
給 reject傳參
我們可以看到,即使是走的失敗回調(diào),下一個成功回調(diào)還是執(zhí)行了,由于 不知道() 默認返回undefined, 相當于失敗已經(jīng)處理了,在成功和失敗都被處理的情況下,下一個回調(diào)會執(zhí)行的。
改成符合預期的,即失敗不調(diào)用。
失敗不調(diào)用的簡寫形式
上述情況執(zhí)行后 .then(除了狗呢)里面的成功回調(diào)沒有執(zhí)行,我們增加一個失敗回調(diào)看看
同樣也可以返回 resolve,讓后面成功回調(diào)可以執(zhí)行
4.3 應用
加載圖片
將圖片的加載寫成一個Promise,一旦加載完成,Promise的狀態(tài)就發(fā)生變化。
const preloadImage = function (path) { return new Promise(function (resolve, reject) { const image = new Image(); image.onload = resolve; image.onerror = reject; image.src = path; }); };
Generator 函數(shù)與 Promise 的結(jié)合(詳情見參考鏈接,阮一峰的教程)
5. 干掉Promise中的回調(diào)
5.1 await
成功的情況
失敗的情況
利用 try catch
await 配合 try catch使用,比較完整
6. 總結(jié)
能利用Promise對象,把普通函數(shù)改成返回Promise的形式,解決回調(diào)地獄的問題。
明白Promise的成功失敗調(diào)用邏輯,可以靈活的進行調(diào)整。
理解核心知識,先用起來,慢慢整合吸收知識。
The above is the detailed content of Example analysis of the principle and use of ES6 Promise. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Vue is a popular front-end framework, and you often encounter various errors and problems when developing applications. Among them, Uncaught(inpromise)TypeError is a common error type. In this article, we will discuss its causes and solutions. What is Uncaught(inpromise)TypeError? Uncaught(inpromise)TypeError error usually appears in

async is es7. async and await are new additions to ES7 and are solutions for asynchronous operations; async/await can be said to be syntactic sugar for co modules and generator functions, solving js asynchronous code with clearer semantics. As the name suggests, async means "asynchronous". Async is used to declare that a function is asynchronous; there is a strict rule between async and await. Both cannot be separated from each other, and await can only be written in async functions.

For browser compatibility. As a new specification for JS, ES6 adds a lot of new syntax and API. However, modern browsers do not have high support for the new features of ES6, so ES6 code needs to be converted to ES5 code. In the WeChat web developer tools, babel is used by default to convert the developer's ES6 syntax code into ES5 code that is well supported by all three terminals, helping developers solve development problems caused by different environments; only in the project Just configure and check the "ES6 to ES5" option.

In daily life, we often encounter problems between promises and fulfillment. Whether in a personal relationship or a business transaction, delivering on promises is key to building trust. However, the pros and cons of commitment are often controversial. This article will explore the pros and cons of commitments and give some advice on how to keep your word. The promised benefits are obvious. First, commitment builds trust. When a person keeps his word, he makes others believe that he is a trustworthy person. Trust is the bond established between people, which can make people more

In es5, you can use the for statement and indexOf() function to achieve array deduplication. The syntax "for(i=0;i<array length;i++){a=newArr.indexOf(arr[i]);if(a== -1){...}}". In es6, you can use the spread operator, Array.from() and Set to remove duplication; you need to first convert the array into a Set object to remove duplication, and then use the spread operator or the Array.from() function to convert the Set object back to an array. Just group.

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a

In es6, the temporary dead zone is a syntax error, which refers to the let and const commands that make the block form a closed scope. Within a code block, before a variable is declared using the let/const command, the variable is unavailable and belongs to the variable's "dead zone" before the variable is declared; this is syntactically called a "temporary dead zone". ES6 stipulates that variable promotion does not occur in temporary dead zones and let and const statements, mainly to reduce runtime errors and prevent the variable from being used before it is declared, resulting in unexpected behavior.

In ES6, you can use the length attribute of the array object to determine how many items there are in the array, that is, to get the number of elements in the array; this attribute can return the number of elements in the array, just use the "array.length" statement. Returns a value representing the number of elements of the array object, that is, the length value.
