使用async和await
var fn = async function(promiseArr) { for(let i = 0,len = arr.length; i<len; i++) { currentPromise = (promiseArr[i] instanceOf Promise) "htmlcode">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)]; // 相当于 // 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( {} ) ); };# 执行结果
{ 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) { // 第一个初始promise console.log( data ); } return promise( item, data.rep ); } ); } ); // 最后一个promise temporary.then( data => console.log( data ) ); };用 map 遍历时,需要过滤初始promise的返回值,并且在遍历结束后,需手动执行最后以后一个promise,否则就会变成如下结果
# 执行结果
{}
{ 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 }以上结果明显不是我们所需要的,但是需要手动过滤第一个promise和执行最后一个promise,会增项不必要的代码量和出错率 后将 mapPromise 修改如下,其原理和Array.prototype.reduce+Promise版本类似
const mapPromise = ( arr ) => { let temporary = Promise.resolve( {} ); arr.map( ( item, index ) => { temporary = temporary.then( ( data ) => { // if (i !== 0) { // // 第一个promise // console.log( data ); // } return new Promise( ( resolve, reject ) => { promise( item, data.rep ).then( data => { console.log( data ); resolve( data ); } ); } ); } ); } ); // 最后一个promise // temporary.then( d => console.log( d ) ); };其他
Array.prototype.forEach、Array.prototype.filter、Array.prototype.some、Array.prototype.every等方法和Array.prototype.map类似,就不过多赘述
以上就是JavaScript实现串行请求的示例代码的详细内容,更多关于JavaScript实现串行请求的资料请关注其它相关文章!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 张琍敏2006-金鼎奖系列14[台湾版]CD1[WAV+CUE]
- 林一峰2008-城市旅人[香港首版][WAV+CUE]
- 群星1990-六个梦电视原声带[日本版][WAV+CUE]
- [In-Akustik7801]KissedByASong-《Dynaudio丹拿发烧示范盘》(2014)HQCD[WAV+CUE]
- 群星《17号音乐仓库2 第10期》[320K/MP3][66.53MB]
- 群星《17号音乐仓库2 第10期》[FLAC/分轨][163.67MB]
- 宝丽金50周年《古典篇》3CD限定盘[低速原抓WAV+CUE][1.8G]
- 群星.1986-新格名曲精选-歌3【新格】【WAV+CUE】
- 【ABC唱片】徐刚《独步外滩SACD》ISO+WAV分轨
- 刘月宁《扬琴独奏-木兰辞变奏曲》[FLAC+CUE]
- 陈奕迅《48首选》[WAV分轨][1.8G]
- 林忆莲《陪着我走》 SACD DSD64[DSF][1.1G]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 刘欢范琳琳.1988-信天游迪斯科(TP版)【内蒙古音像】【WAV+CUE】
- 群星-真声音真音乐[WAV+CUE]