JS PromiseLike的判定與使用詳解
一. $.ajax()返回值遇到的問題
當(dāng)我們執(zhí)行如下js代碼時,可以看到$.ajax()執(zhí)行后,得到的response對象并不為空,并且response對象的responseJSON屬性也確實是有值的。
但是,當(dāng)我們執(zhí)行response.responseJSON后,得到的居然是undefined。
并且我們使用await 對response對象等待后,得到的就直接是response.responseJSON中的值。
setTimeout(async () => {
const response = $.ajax({
url: "https://api.github.com/users/fengyehong123",
type: 'GET',
});
console.log(response);
console.log(response.responseJSON); // undefined
const result = await response;
console.log(result);
}, 1000);
執(zhí)行效果如下:

上述現(xiàn)象是因為 $.ajax()得到的對象是一個 Promise Like對象,Promise Like對象和ES6的new Promise()一樣,都是對 Promise A+ 規(guī)范的實現(xiàn),因此可以使用 await 進行等待。
二. Promise A+ 規(guī)范
官網(wǎng): https://promisesaplus.com/
ES6的new Promise()也好,$.ajax()函數(shù)返回的Promise Like對象也好,
都只是Promise A+規(guī)范的一種實現(xiàn),該規(guī)范告訴我們?nèi)绾巫约簩崿F(xiàn)一個Promise。
三. 判斷是否為PromiseLike
如果一個值的類型為 object 或者 function并且 該值還存在一個then方法那么 該值就是一個 PromiseLike 對象。
// 判斷是否為 Promise Like
function isPromiseLike(value) {
if(value === null) {
return false;
}
if ((typeof value === 'object' || typeof value === 'function') && (typeof value.then === 'function')){
return true;
}
return false;
}
3.1 判斷ES6的new Promise()
ES6 的 new Promise() 是 Promise A+ 規(guī)范的實現(xiàn),所以肯定是一個 PromiseLike 對象
const promise_obj = new Promise((resolve, reject) => {
resolve('楓葉紅');
});
console.log(isPromiseLike(promise_obj) ? "promise_obj是PromiseLike對象" : "promise_obj非PromiseLike對象");
3.2 判斷包含then方法的對象
定義一個對象,對象里面有一個then方法,方法里面是耗時操作。符合該對象是一個Promise Like對象。
const then_response = {
then: function(resolve, reject) {
setTimeout(() => {
resolve('賈飛天');
}, 1000)
}
}
console.log(
isPromiseLike(then_response)
? "then_response是PromiseLike對象" : "then_response非PromiseLike對象"
);
// then_response是PromiseLike對象
(async (response) => {
/*
此處的response實際上是then_response
因為 then_response 是一個 Promise Like 對象
要想await的話,必須包裹在 函數(shù)中
因此此處定義了一個立即執(zhí)行函數(shù),還可以避免給函數(shù)取名的麻煩
*/
const result = await response;
console.log(result);
})(then_response);
3.3 判斷$.ajax()返回的對象
// ?兩秒之后發(fā)送ajax請求
setTimeout(async () => {
const response = $.ajax({
url: "https://api.github.com/users/fengyehong123",
type: 'GET',
});
// 是一個PromiseLike對象
console.log(
isPromiseLike(response) ? "response是PromiseLike對象" : "response非PromiseLike對象"
);
// response是PromiseLike對象
// 正因為是 PromiseLike對象 ,所以才可以進行await
const result = await response;
console.log(result);
}, 2000);
也就是說,我們之后的$.ajax()函數(shù)可以這么寫
// ajax的請求對象
const jqRequest = $.ajax({
url,
method: 'GET'
});
// doneCallBack,failCallBack,alwaysCallback 是從外部傳入的回調(diào)函數(shù)
jqRequest.done(function(data, textStatus, jqXHR) {
doneCallBack && doneCallBack(data);
}).fail(function(jqXHR, textStatus, errorThrown) {
failCallBack && failCallBack();
}).always(function() {
alwaysCallback && alwaysCallback();
});
也可以這么寫,從而可以避免回調(diào)的方式
document.querySelector('#btn').addEventListener('click', async function() {
const url = "https://api.github.com/users/fengyehong123";
// 后端的返回值
let result = null;
try {
result = await $.ajax({
url,
type: 'GET',
});
} catch (error) {
const {responseJSON} = error;
console.log(`請求失敗!原因是: ${responseJSON}`);
} finally {
console.log("請求完成!");
}
if(!result) {
// 進行相應(yīng)的業(yè)務(wù)處理
return;
}
console.log("返回的最終值為:");
console.log(result);
});到此這篇關(guān)于JS PromiseLike的判定與使用詳解的文章就介紹到這了,更多相關(guān)JS PromiseLike內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 深入探討JavaScript異步編程中Promise的關(guān)鍵要點
- 詳解JavaScript中Promise的原理與應(yīng)用
- JavaScript使用Promise控制并發(fā)請求
- 在JavaScript中使用Promise.allSettled()的方法
- Javascript?promise.all的用法介紹(簡潔易懂)
- 詳解如何使用JavaScript中Promise類實現(xiàn)并發(fā)任務(wù)控制
- JS手搓P(guān)romise的常見方法總結(jié)
- 使用Promise和JavaScript有效處理1000個請求的方法
- JavaScript 中使用Promise.all()方法經(jīng)驗技巧詳解
相關(guān)文章
原生JS實現(xiàn)H5轉(zhuǎn)盤游戲的示例代碼
這篇文章主要介紹了如何利用原生JS實現(xiàn)轉(zhuǎn)盤游戲,可以自由調(diào)整概率。文中的示例代碼講解詳細,對我們學(xué)習(xí)JavaScript有一定幫助,需要的可以參考一下2022-03-03
spirngmvc js傳遞復(fù)雜json參數(shù)到controller的實例
下面小編就為大家分享一篇spirngmvc js傳遞復(fù)雜json參數(shù)到controller的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
神級程序員JavaScript300行代碼搞定漢字轉(zhuǎn)拼音
這篇文章主要介紹了神級程序員JavaScript300行代碼搞定漢字轉(zhuǎn)拼音,需要的朋友可以參考下2017-05-05
利用JS對iframe父子(內(nèi)外)頁面進行操作的方法教程
這篇文章主要給大家介紹了利用JS對iframe父子(內(nèi)外)頁面進行操作的方法教程,其中包括了怎么對iframe進行操作、在iframe里面控制iframe外面的js代碼以及在父框架對子iframe進行操作等,需要的朋友可以參考借鑒。2017-06-06
JsRender for index循環(huán)索引用法詳解
這篇文章主要介紹了JsRender for index循環(huán)索引用法,以實例形式詳細分析了JsRender中循環(huán)的用法,需要的朋友可以參考下2014-10-10

