我正在学习承诺,并且我尝试使用以下代码链接它们。在此,首先执行Connect Error (1044) Access denied for user 'user'@'%' to database 'name'
:
.then()
这是输出:
function myPromise1()
{
console.log("Promise", 1);
return Promise.all([
new Promise((resolve, reject) => {
setTimeout(() => {
console.log("This should be before 'Yay!' for 1");
resolve(1);
}, 3000);
}),
new Promise((resolve, reject) => {
setTimeout(() => {
console.log("This should be before 'Yay!' for 2");
resolve(2);
}, 3000);
})
]);
}
function myPromise2() {
console.log("Promise", 2);
var arr = [1,2,3,4];
return Promise.all(arr.map(i => {
new Promise((resolve, reject) => {
setTimeout(() => {
console.log("This should be before 'Yay!' for i= " + i);
resolve("Success!" + i);
}, 3000)
})
}));
}
function anotherPromise(val) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(val);
}, 3000);
});
}
myPromise2()
.then((msg) => {
console.log("Yay! " + msg);
return anotherPromise("Awesome!");
})
.then((msg2) => {
console.log("This should be Awesome:", msg2);
})
.catch((err) => {
console.log("Uh no, this is a lie --->", err);
});
console.log("This will print as second line after the method name, even though its the last line.");
如您所见,Promise 2
This will print as second line after the method name, even though its the last line.
Yay! ,,,
This should be before 'Yay!' for i= 1
This should be before 'Yay!' for i= 2
This should be before 'Yay!' for i= 3
This should be before 'Yay!' for i= 4
This should be Awesome: Awesome!
Process finished with exit code 0
之前被调用,因此不会返回值。
但是,如果我改为呼叫.then()
,我会得到所需的输出。这些值也会被返回,并且在两个承诺之后都会调用myPromise1()
。
如何解决.then()
的问题?
答案 0 :(得分:4)
您忘记在此处使用map
方法返回:
function myPromise2() {
console.log("Promise", 2);
var arr = [1,2,3,4];
return Promise.all(arr.map(i => {
/* missing return ---> */ new Promise((resolve, reject) => {
setTimeout(() => {
console.log("This should be before 'Yay!' for i= " + i);
resolve("Success!" + i);
}, 3000)
})
}));
}
function myPromise1() {
console.log("Promise", 1);
return Promise.all([
new Promise((resolve, reject) => {
setTimeout(() => {
console.log("This should be before 'Yay!' for 1");
resolve(1);
}, 3000);
}),
new Promise((resolve, reject) => {
setTimeout(() => {
console.log("This should be before 'Yay!' for 2");
resolve(2);
}, 3000);
})
]);
}
function myPromise2() {
console.log("Promise", 2);
var arr = [1, 2, 3, 4];
return Promise.all(arr.map(i => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("This should be before 'Yay!' for i= " + i);
resolve("Success!" + i);
}, 3000)
})
}));
}
function anotherPromise(val) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(val);
}, 3000);
});
}
myPromise2()
.then((msg) => {
console.log("Yay! " + msg);
return anotherPromise("Awesome!");
})
.then((msg2) => {
console.log("This should be Awesome:", msg2);
})
.catch((err) => {
console.log("Uh no, this is a lie --->", err);
});
console.log("This will print as second line after the method name, even though its the last line.");

答案 1 :(得分:1)
您忘记return
来自map
回调的承诺,导致Promise.all
被调用4 undefined
个数组:
function myPromise2() {
console.log("Promise", 2);
var arr = [1,2,3,4];
return Promise.all(arr.map(i => {
return new Promise((resolve, reject) => {
// ^^^^^^
setTimeout(() => {
console.log("This should be before 'Yay!' for i= " + i);
resolve("Success!" + i);
}, 3000)
})
}));
}
或者,删除大括号并使其成为一个简洁的主体,隐式返回表达式:
function myPromise2() {
console.log("Promise", 2);
var arr = [1,2,3,4];
return Promise.all(arr.map(i =>
// ^
new Promise((resolve, reject) => {
setTimeout(() => {
console.log("This should be before 'Yay!' for i= " + i);
resolve("Success!" + i);
}, 3000)
})
));
//^
}