Promise.race
Promise.race 方法同样是将多个Promise实例,包装成一个新的Promise实例
race 谁跑的快,以谁为准执行回调(时间为准)
function runAsync1() { var seven = new Promise (function (resolve, reject) { //异步操作 setTimeout(function () { console.log("执行完成1") resolve("柒琪") },3000) }); return seven; } function runAsync2() { var seven = new Promise (function (resolve, reject) { //异步操作 setTimeout(function () { console.log("执行完成2") resolve("柒柒") },2000) }); return seven; } function runAsync3() { var seven = new Promise (function (resolve, reject) { //异步操作 setTimeout(function () { console.log("执行完成3") resolve("柒奇") },2000) }); return seven; } // race 谁跑的快,以谁为准执行回调 (时间为准) // 某个异步请求设置超时时间,并且在超时后执行相应的操作 Promise .race([runAsync1(),runAsync2(),runAsync3()]) .then(function (results) { console.log(results) })
Promise.race()
谁跑的快,以谁为准执行回调 (时间为准)