【发布时间】:2020-02-27 23:08:42
【问题描述】:
我有一个数组,我想在这个数组上使用 Promise:
let check_duplicate_phone = store.state.lines.map(function (item) {
return core.checkIsPhoneDuplicate(item.phone , item.phone_significant);
});
let res = await Promise.all(check_duplicate_phone).then(function(values) {
console.log(values);
});
这就是checkIsPhoneDuplicate 函数:
async checkIsPhoneDuplicate(phone , phone_significant) {
var data = {
'phone': phone ,
'phone_significant' : phone_significant
}
let res = await Axios.post(checkPhoneDuplicate_route, data)
.then(response => {
return response.data;
}).catch(error => console.log(error));
return res;
}
但它不等待响应,只运行下一步。 我知道它应该曾经喜欢 this 并且我已经阅读了这个 answer 但我没有发现我的错误在哪里。
谢谢。
【问题讨论】:
-
一些不起作用的是
res总是undefined,因为这是记录values的then回调的返回值。你最好写const values = await Promise.all(check_duplicate_phone); console.log(values);
标签: javascript promise