域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過
這篇文章主要介紹了JavaScript實(shí)現(xiàn)串行請(qǐng)求的示例代碼,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下
使用async和await
var fn = async function(promiseArr) {
for(let i = 0,len = arr.length; i<len; i++) {
currentPromise = (promiseArr[i] instanceOf Promise) ? promiseArr[i] : Promise.resolve(promiseArr[i]);
var result = await currentPromise;
console.log(result)
}
}
fn(arr)
Promise實(shí)現(xiàn)
依照 promises 規(guī)范,一旦一個(gè) promise 被創(chuàng)建,它就被執(zhí)行了。如果then方法里返回的是一個(gè)promise對(duì)象,那么執(zhí)行下一個(gè)then 的時(shí)候必定是在上一個(gè)then執(zhí)行完之后執(zhí)行。
關(guān)鍵點(diǎn)在于then的時(shí)候再創(chuàng)建
var createPromise = function(time) {
return (resolve, reject)=> {
return new Promise((resolve, reject)=>{
setTimeout(()=>{
console.log('timein'+time)
resolve();
}, time*1000)
})
}
}
function serpromise(arr) {
arr.reduce((pre, next, index, carr)=>{
return pre.then(next)
}, Promise.resolve())
}
var arr=[createPromise(2),createPromise(1),createPromise(3),createPromise(4),createPromise(5)];
// 相當(dāng)于
// Promise.resolve().then(createPromise(2)).then(createPromise(1))......
serpromise(arr)
Array.prototype.reduce + async/await 版本
const reduceAsync = ( arr ) => {
arr.reduce( async ( prev, curr ) => {
const { rep } = await prev;
const obj = await promise( curr, rep );
console.log( obj );
return obj;
}, Promise.resolve( {} ) );
};
Array.prototype.reduce + Promise 版本
const reducePromise = ( arr ) => {
arr.reduce( ( prev, curr ) => {
return prev.then( data => {
return new Promise( ( resolve, reject ) => {
promise( curr, data.rep ).then( res => {
console.log( res );
resolve( res );
} );
} );
} );
}, Promise.resolve( {} ) );
};
# 執(zhí)行結(jié)果
{ req: 'PM:04:49:08', rep: 'PM:04:49:11', item: 1 }
{ req: 'PM:04:49:11', rep: 'PM:04:49:14', item: 2 }
{ req: 'PM:04:49:14', rep: 'PM:04:49:17', item: 3 }
{ req: 'PM:04:49:17', rep: 'PM:04:49:20', item: 4 }
Array.prototype.map + Promise 版本
const mapPromise = ( arr ) => {
let temporary = Promise.resolve( {} );
arr.map( ( item, index ) => {
temporary = temporary.then( ( data ) => {
if (i !== 0) {
// 第一個(gè)初始promise
console.log( data );
}
return promise( item, data.rep );
} );
} );
// 最后一個(gè)promise
temporary.then( data => console.log( data ) );
};
用 map 遍歷時(shí),需要過濾初始promise的返回值,并且在遍歷結(jié)束后,需手動(dòng)執(zhí)行最后以后一個(gè)promise,否則就會(huì)變成如下結(jié)果
# 執(zhí)行結(jié)果
{}
{ req: 'PM:04:49:08', rep: 'PM:04:49:11', item: 1 }
{ req: 'PM:04:49:11', rep: 'PM:04:49:14', item: 2 }
{ req: 'PM:04:49:14', rep: 'PM:04:49:17', item: 3 }
以上結(jié)果明顯不是我們所需要的,但是需要手動(dòng)過濾第一個(gè)promise和執(zhí)行最后一個(gè)promise,會(huì)增項(xiàng)不必要的代碼量和出錯(cuò)率 后將 mapPromise 修改如下,其原理和Array.prototype.reduce+Promise版本類似
const mapPromise = ( arr ) => {
let temporary = Promise.resolve( {} );
arr.map( ( item, index ) => {
temporary = temporary.then( ( data ) => {
// if (i !== 0) {
// // 第一個(gè)promise
// console.log( data );
// }
return new Promise( ( resolve, reject ) => {
promise( item, data.rep ).then( data => {
console.log( data );
resolve( data );
} );
} );
} );
} );
// 最后一個(gè)promise
// temporary.then( d => console.log( d ) );
};
其他
Array.prototype.forEach、Array.prototype.filter、Array.prototype.some、Array.prototype.every等方法和Array.prototype.map類似,就不過多贅述
以上就是JavaScript實(shí)現(xiàn)串行請(qǐng)求的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于JavaScript實(shí)現(xiàn)串行請(qǐng)求的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
來源:腳本之家
鏈接:https://www.jb51.net/article/195555.htm
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!