在开发中经常遇到,需要经过几次网络请求,且结果都正确才可以执行一下步的操作,在没有es6 promise之前我们通常使用标记来记录状态的方法进行处理,操作相当繁琐且不好控制,ES6 为我们提供了Promise.all()方法,下面是具体的案例:
Promise.all([ new Promise(res=>{ setTimeout(function () { resolve("res1") },1000) }), new Promise(res=>{ setTimeout(function () { resolve("res2") },1000) }) ]).then(results=>{ console.log(results); //这是上面两个返回的数组 })