ES6中Promise.race
<script type="text/javascript">
let p1 = new Promise(function(resolve,reject){
setTimeout(function(){
resolve("450毫秒后执行")
},450)
});
let p2 = new Promise(function(resolve,reject){
setTimeout(function(){
resolve("300毫秒后执行")
},300)
});
let p3 = new Promise(function(resolve,reject){
setTimeout(function(){
resolve("400毫秒后执行")
},400)
});
let p4 = new Promise(function(resolve,reject){
setTimeout(function(){
resolve("500毫秒后执行")
},500)
});
let arr = [p1,p2,p3,p4];
//有多个异步操作,只有1个返回给结果,就立即执行.
let result = Promise.race(arr);
result.then(function(res){
console.log(res)
})
</script>