CNode

一个关于promise的问题!!!

问答
MMeetTheBest发布于8 年前最后回复8 年前6 回复4115 浏览0 收藏

请大佬们帮忙看看, 那个index为啥每次都是最后一次的值(第五行的那个index).

promise_problem.png

查看回复

回复 (6)

L
LeoChowChina#1·8 年前

看一下闭包,就理解了, 要把index传入

M
MeetTheBest#2·8 年前
引用 LeoChowChina看一下闭包,就理解了, 要把index传入

@LeoChowChina 您好, 多谢您的帮忙, 不过我通过 添加一个自执行函数 好像也没有效果

((index) => {
	// code
})(index)
W
wenjiasen#3·8 年前
const resolvePromise = Promise.resolve();
const pormiseAll = [1, 2, 3, 4].map((item, index) => {
	return resolvePromise.then(async res => {
		return item * index;
	});
});

Promise.all(pormiseAll).then(res => {
	console.log(res);
});
//[ 0, 2, 6, 12 ]
H
halu886#4·8 年前

在第三行添加 let fixIndex = index; 把第五行index改为fixIndex;

M
MeetTheBest#5·8 年前
引用 wenjiasen

@wenjiasen 多谢,指导

参与回复
登录后即可参与回复。登录