JS | Promise.all

Testing

function getURL(URL) {
	 return new Promise(function (resolve, reject) {
		 var req = new XMLHttpRequest();
		 req.open('GET', URL, true);
		 req.onload = function () {
			 if (req.status === 200) {
			 	resolve(req.responseText);
			 } else {
			 	reject(new Error(req.statusText));
			 }
		 };
		 req.onerror = function () {
		 	reject(new Error(req.statusText));
		 };
		 req.send();
	 });
}
var request = {
	 comment: function getComment() {
	 	return getURL('http://azu.github.io/promises-book/json/comment.json').then(JSON.parse);
	 },
	 people: function getPeople() {
	 	return getURL('http://azu.github.io/promises-book/json/people.json').then(JSON.parse);
	 }
 };
function main() {
 	function recordValue(results, value) {
		 results.push(value);
		 return results;
	 }
	 // [] 用来保存初始化的值
	 var pushValue = recordValue.bind(null, []);
	 return request.comment().then(pushValue).then(request.people).then(pushValue);
}
// 运行示例
main().then(function (value) {
	 	console.log(value);
}).catch(function(error){
	 	console.log(error);
});

在这里插入图片描述

Promise.all
function getURL(URL) {
	 return new Promise(function (resolve, reject) {
		 var req = new XMLHttpRequest();
		 req.open('GET', URL, true);
		 req.onload = function () {
			 if (req.status === 200) {
			 	resolve(req.responseText);
			 } else {
			 	reject(new Error(req.statusText));
			 }
		 };
		 req.onerror = function () {
		 	reject(new Error(req.statusText));
		 };
		 req.send();
	 });
}
var request = {
	 comment: function getComment() {
	 	return getURL('http://azu.github.io/promises-book/json/comment.json').then(JSON.parse);
	 },
	 people: function getPeople() {
	 	return getURL('http://azu.github.io/promises-book/json/people.json').then(JSON.parse);
	 }
 };
function main() {
 	return Promise.all([request.comment(), request.people()]);
}
// 运行示例
main().then(function (value) {
	 	console.log(value);
}).catch(function(error){
	 	console.log(error);
});
Execution order

Test

// `delay`毫秒后执行resolve
function timerPromisefy(delay) {
	 return new Promise(function (resolve) {
		 setTimeout(function () {
		 	resolve(delay);
		 }, delay);
	 });
}
var startDate = Date.now();
// 所有promise变为resolve后程序退出
Promise.all([
	 timerPromisefy(1),
	 timerPromisefy(32),
	 timerPromisefy(64),
	 timerPromisefy(128)
 ]).then(function (values) {
 	 console.log(Date.now() - startDate + 'ms');
	 // 約128ms
	 console.log(values); // [1,32,64,128]
});

timerPromisefy 会每隔一定时间(通过参数指定)之后,返回一个promise对象,状态为FulFilled,其状态值为传给 timerPromisefy 的参数。而传给 Promise.all 的则是由上述promise组成的数组。

var promises = [
	 timerPromisefy(1),
	 timerPromisefy(32),
	 timerPromisefy(64),
	 timerPromisefy(128)
];

这时候,每隔1, 32, 64, 128 ms都会有一个promise发生 resolve 行为。也就是说,这个promise对象数组中所有promise都变为resolve状态的话,至少需要128ms。

实际我们计算一下 Promise.all 的执行时间的话,它确实是消耗了128ms的时间。

从上述结果可以看出,传递给 Promise.all 的promise并不是一个个的顺序执行的,而是同时开始、并行执行的
如果这些promise全部串行处理的话,那么需要 等待1ms → 等待32ms→ 等待64ms → 等待128ms ,全部执行完毕需要225ms的时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值