Javascript 等待Promise.all继续执行
我想将产品插入阵列,然后继续使用该阵列。我没有使用期票的到期日。下面是我的密码。 这是我想要返回数组的函数Javascript 等待Promise.all继续执行,javascript,promise,Javascript,Promise,我想将产品插入阵列,然后继续使用该阵列。我没有使用期票的到期日。下面是我的密码。 这是我想要返回数组的函数 const findProduct = async function(request) { const products = []; await Promise.all( request.products.forEach(async product => { await db .get() .collection('pro
const findProduct = async function(request) {
const products = [];
await Promise.all(
request.products.forEach(async product => {
await db
.get()
.collection('products')
.findOne({ _id: product.productId.toString() }, (err, result) => {
if (err) throw new Error('exception!');
products.push(result);
});
})
)
.then(() => {
return products;
})
.catch(e => e);
};
产品总是未定义的
我该不该把承诺还给你。我把它保存到一个数组中,然后使用那个数组
这就是我计划使用数组的方式
const purchase = new Purchase(req.body);
const products = await findProduct(req.body);
purchase.products = [...products];
Promise.all()
需要一个Promises数组,map将返回一个数组。由于db
也将返回一个承诺,我们只需将这些承诺输入Promise.all()
编辑:忘记返回Promise.all()
因为很多人会问:“我如何从承诺中获得一些东西?”-->使用resolve值
这里有一个很长的解释,解释了为什么这样做:wait()
将返回一个承诺,该承诺映射到一个数组中,并使用map()
返回该数组。因此,映射完成后,我们有一个数组,其中包含由3个wait()
调用返回的3个承诺。此数组被传递到Promise.all()
中,它将等待解析所有承诺,并将自身解析为一个包含该数组中承诺的所有解析值的数组。由于findProduct()
将从Promise.all()
返回此承诺,因此可以使用findProduct()访问它。然后()
或等待findProduct()
编辑:回答有关的评论。然后()
返回承诺
findProduct(start)
.then(res => console.log(res))
.then(res => console.log(res))
// logs [ 'milk', 'butter', 'eggs' ] and undefined
findProduct(start)
.then(res => {console.log(res); return res})
.then(res => console.log(res))
// logs 2x [ 'milk', 'butter', 'eggs' ]
Promise.all()
需要一个Promises数组,map将返回一个数组。由于db
也将返回一个承诺,我们只需将这些承诺输入Promise.all()
编辑:忘记返回Promise.all()
因为很多人会问:“我如何从承诺中获得一些东西?”-->使用resolve值
这里有一个很长的解释,解释了为什么这样做:wait()
将返回一个承诺,该承诺映射到一个数组中,并使用map()
返回该数组。因此,映射完成后,我们有一个数组,其中包含由3个wait()
调用返回的3个承诺。此数组被传递到Promise.all()
中,它将等待解析所有承诺,并将自身解析为一个包含该数组中承诺的所有解析值的数组。由于findProduct()
将从Promise.all()
返回此承诺,因此可以使用findProduct()访问它。然后()
或等待findProduct()
编辑:回答有关的评论。然后()
返回承诺
findProduct(start)
.then(res => console.log(res))
.then(res => console.log(res))
// logs [ 'milk', 'butter', 'eggs' ] and undefined
findProduct(start)
.then(res => {console.log(res); return res})
.then(res => console.log(res))
// logs 2x [ 'milk', 'butter', 'eggs' ]
在findProduct
函数的上下文中,如果您打算使用const products=await findProduct(req.body)等待findProduct
的结果,则使用async
/await
语法没有多大意义代码>
相反,您希望仅使用承诺来定义函数
首先,db.collection.findOne()。可以这样做:
新承诺((解决、拒绝)=>{
分贝
.get()
.集合(“产品”)
.findOne({u id:product.productId.toString()},(err,result)=>{err?reject(err):resolve(result)})
});
这里的(err,result)=>{err?reject(err):resolve(result)}
只是
(错误,结果)=>{
如果(错误){
拒绝(错误);
}否则{
决心(结果);
}
}
接下来,必须将数组传递给Promise.all()
。因为您已经有了数组request.products
,所以您可以将每个值转换(或映射)为相应的承诺,以获取其数据。这是通过使用。当以这种方式使用时,Promise.all()
将按照与request.products
相同的顺序解析为产品数据对象数组
将其混合到代码中,可以产生:
const findProduct=函数(请求){
回报你的承诺(
request.products.map(product=>{
返回新承诺((解决、拒绝)=>{
分贝
.get()
.集合(“产品”)
.findOne({u id:product.productId.toString()},(err,result)=>err?拒绝(err):解析(result))
});
})
);
};
请注意,我删除了。然后(()=>产品)
(因为上面的承诺,Promise.all将返回产品本身)和.catch(e=>e)
(因为您不应该忽略这样的错误)
通过这些更改,您现在可以使用findProduct
,就像您在问题中使用它一样
更新:
MongoDB API似乎符合承诺,这意味着我们可以删除承诺回调包装,并将其简化为:
const findProduct=函数(请求){
回报你的承诺(
request.products.map(product=>{
返回数据库
.get()
.集合(“产品”)
.findOne({u id:product.productId.toString()});
})
);
};
在您的findProduct
函数的上下文中,如果您打算使用const products=wait findProduct(req.body)等待findProduct
的结果,则使用async
/wait
语法没有多大意义代码>
相反,您希望仅使用承诺来定义函数
首先,db.collection.findOne()。可以这样做:
新承诺((解决、拒绝)=>{
分贝
.get()
.集合(“产品”)
.findOne({u id:produ
findProduct(start)
.then(res => console.log(res))
.then(res => console.log(res))
// logs [ 'milk', 'butter', 'eggs' ] and undefined
findProduct(start)
.then(res => {console.log(res); return res})
.then(res => console.log(res))
// logs 2x [ 'milk', 'butter', 'eggs' ]