Returns the final value when the request has no dependencies

Promise. AllSettled oblivious

Function ajaxFun(time = 1000, index) {return new Promise((resolve) => {setTimeout(() => {console.log(' time: ', time, 'index: ',index); Resolve ({time, text: 'execute'}); }, time) }) } function ajaxFunFalse(time = 1000, index) { return new Promise((resolve, Reject = > {setTimeout () () = > {the console. The log (' execution - time: 'time,' the index: 'index); Reject ({time, text: 'reject'}); }, time)})} setTimeout () = > {the console. The log (' execution - time: '200,' index: ', 999); Resolve ({time, text: 'execute'}); }, 200) const httpArr = [ajaxFunFalse(100, 1), ajaxFun(1000, 2), ajaxFun(200, 3)]; Promise.allsettled (httpArr).then(res => {console.log(res, 'last execution '); })Copy the code

for await of

Function ajaxFun(time = 1000, index) {return new Promise((resolve) => {setTimeout(() => {console.log(' time: ', time, 'index: ',index); Resolve ({time, text: 'execute'}); }, time)})} setTimeout () = > {the console. The log (' execution - time: '200,' index: ', 999); Resolve ({time, text: 'execute'}); }, 200) const httpArr = [ajaxFun(100, 1), ajaxFun(1000, 2), ajaxFun(200, 3)]; async function carryOut() { const list = []; for await(const item of httpArr) { console.log(item); list.push(item); } return list; } carryOut().then(res => {console.log(res, 'last execution '); })Copy the code

promise.all

Function ajaxFun(time = 1000, index) {return new Promise((resolve) => {setTimeout(() => {console.log(' time: ', time, 'index: ',index); Resolve ({time, text: 'execute'}); }, time)})} setTimeout () = > {the console. The log (' execution - time: '200,' index: ', 999); Resolve ({time, text: 'execute'}); }, 200) const httpArr = [ajaxFun(100, 1), ajaxFun(1000, 2), ajaxFun(200, 3)]; Promise.all(httpArr).then(res => {console.log(res, 'last execution '); })Copy the code

Request exists dependency, resolve callback hell

generator

Function ajaxFun(time = 1000, index, CD) {setTimeout(() => {console.log(' time: ', time, 'index: ',index); if (! cd) return; CD ({time, text: 'execute'}); }, time) } let list = [] const gen = generatorFun(); function* generatorFun() { yield ajaxFun(1000, 1, (data) => { list.push(data) gen.next(); }); yield ajaxFun(1000, 2, (data) => { list.push(data) gen.next(); }); yield ajaxFun(1000, 3, (data) => { list.push(data) gen.next(); }); return ajaxFun(1000, 4, (data) => { list.push(data) console.log(list) }); } gen.next()Copy the code

The time: 1000 index: 1 command is executed. The time: 1000 index: 2 command is executed. The time: 1000 index: 3 command is executed

Other

promise.any

Any promise returns directly with success

promise.race

Any promise completion returns directly