這篇文章主要講解了“JavaScript怎么處理并行請(qǐng)求”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“JavaScript怎么處理并行請(qǐng)求”吧!
兩個(gè)異步請(qǐng)求同時(shí)發(fā)出,兩個(gè)請(qǐng)求都返回時(shí)再做處理
這里的方法僅提供思路,只做請(qǐng)求成功處理
方法一
使用Promise.all
const startTime = new Date().getTime() function request(time) { return new Promise(resolve => { setTimeout(() => { resolve(time) }, time) }) } let request1 = request(3000) let request2 = request(2000) Promise.all([request1, request2]).then(res => { console.log(res, new Date() - startTime) // [ 3000, 2000 ] 3001 })
方法二
自定義狀態(tài),在回調(diào)中判斷返回狀態(tài),待2個(gè)請(qǐng)求都有返回值時(shí)再做處理
const startTime = new Date().getTime() function request(time) { return new Promise(resolve => { setTimeout(() => { resolve(time) }, time) }) } let state = [undefined, undefined] let request1 = request(3000) let request2 = request(2000) request1.then(res => { state[0] = res process() }) request2.then(res => { state[1] = res process() }) function process() { if (state[0] && state[1]) { console.log(state, new Date() - startTime) // [ 3000, 2000 ] 3001 } }
方法三
generator,yield
const startTime = new Date().getTime() function ajax(time, cb) { setTimeout(() => cb(time), time) } function request(time) { ajax(time, data => { it.next(data); }) } function* main() { let request1 = request(3000); let request2 = request(2000); let res1 = yield request1 let res2 = yield request2 console.log(res1, res2, new Date() - startTime) // 2000 3000 3001 } let it = main(); it.next();
這個(gè)地方有點(diǎn)問(wèn)題,因?yàn)閞equest2耗時(shí)較短,會(huì)先返回,
也就是先執(zhí)行it.next(2000),導(dǎo)致res1獲得了request2的返回值
若使用co函數(shù),則不會(huì)存在這個(gè)問(wèn)題,因?yàn)閏o是在promise.then函數(shù)中才執(zhí)行it.next(),相當(dāng)于it.next()是鏈?zhǔn)秸{(diào)用
generator使用co函數(shù)
const co = require('co') const startTime = new Date().getTime() function request (time) { return new Promise(resolve => { setTimeout(() => { resolve(time) }, time) }) } co(function* () { let request1 = request(3000); let request2 = request(2000); let res1 = yield request1 let res2 = yield request2 console.log(res1, res2, new Date() - startTime) // 3000 2000 3001 })
有了co函數(shù),就不需要生成it和執(zhí)行next方法了; co的原理其實(shí)也簡(jiǎn)單,就是遞歸執(zhí)行next,直到done為true; 如果next返回的value是Promise,則在then函數(shù)中執(zhí)行next,若不是Promise,直接執(zhí)行next函數(shù) 下面是co函數(shù)的簡(jiǎn)版手寫(xiě)實(shí)現(xiàn)
function co(func) { let it = func() let t = it.next() next() function next() { if (t.done) return if (t.value instanceof Promise) { t.value.then(res => { t = it.next(res) next() }) } else { t = it.next(t.value) next() } } }
方法四
有了generator,很容易想到async/await,畢竟async/await就是由generator實(shí)現(xiàn)的
// setTimeout模擬異步請(qǐng)求,time為請(qǐng)求耗時(shí) const startTime = new Date().getTime() function request (time) { return new Promise(resolve => { setTimeout(() => { resolve(time) }, time) }) } (async function () { let request1 = request(3000) let request2 = request(2000) let res1 = await request1 console.log(res1, new Date() - startTime) // 3000 3001 let res2 = await request2 console.log(res2, new Date() - startTime) // 2000 3005 })()
感謝各位的閱讀,以上就是“JavaScript怎么處理并行請(qǐng)求”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)JavaScript怎么處理并行請(qǐng)求這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。