Promise 在每个对象数组中处于挂起状态
Promise is in pending while in each array of object
我想在对象数组中执行 promise,并在同一数组中用 promise 响应替换数组中的路径值,我正在尝试以下代码:
const cloudinary = require('cloudinary')
const cloudinaryConfig = require('../configs/cloudConfig.json')
cloudinary.config({
cloud_name: cloudinaryConfig.cloud_name,
api_key: cloudinaryConfig.api_key,
api_secret: cloudinaryConfig.api_secret
})
fileArray = [
{
fieldname: 'productThumbImage',
originalname: 'Boyka.png',
path: '/home/rahul/MaxDigiAssignment/mxNodeEcommerce/mx-ecommercenode/upload/product/1589983049420.png'
},
{
fieldname: 'productPhoto',
originalname: 'Code.png',
filename: '1589983049436.png',
path: '/home/rahul/MaxDigiAssignment/mxNodeEcommerce/mx-ecommercenode/upload/product/1589983049436.png'
},
{
fieldname: 'productPhoto',
originalname: 'Boyka.png',
filename: '1589983049438.png',
path: '/home/rahul/MaxDigiAssignment/mxNodeEcommerce/mx-ecommercenode/upload/product/1589983049438.png'
}
]
const files = imgs.map(async (img)=>{
let path = await cloudinary.v2.uploader.upload(img.path)
return {
...img,
filePath: path.url
}
})
console.log('files 11/// ',await files);
但我收到错误提示{ }
我想在每个对象中执行这个承诺然后得到响应
我预期的数组如下:
[
{
fieldname: 'productThumbImage',
originalname: 'Boyka.png',
path:'http://res.cloudinary.com/deqpxepbs/image/upload/v1589982424/ywfetodkvyadmdqjof1i.png'
},
{
fieldname: 'productPhoto',
originalname: 'Code.png',
filename: '1589983049436.png',
path:'http://res.cloudinary.com/deqpxepbs/image/upload/v1589982424/desb18dgungva5y8apbv.png'
},
{
fieldname: 'productPhoto',
originalname: 'Boyka.png',
filename: '1589983049438.png',
path:'http://res.cloudinary.com/deqpxepbs/image/upload/v1589982424/ugud4flh7gfl3ymyh0xx.png'
}
]
files
将是一系列承诺,而不是单个承诺。你应该:
console.log('files 11/// ',await Promise.all(files));
您想做的是:
- map
你的图像数组到 Promise
s 的数组
- await
你所有的 Promise
并将它们的结果作为 paths/URLs 的数组
- map
对象数组的路径数组。
const getNewFiles = async (imgs) => {
const promises = imgs.map(img => {
return cloudinary.v2.uploader.upload(img.path)
})
const paths = await Promise.all(promises)
/* If you're using Node.js version 12 or more recent, a better
alternative to `Promise.all` is `Promise.allSettled` */
// const paths = await Promise.allSettled(promises)
const newFiles = paths.map((path, index) => {
return {
...imgs[index],
path: path.url
}
})
return newFiles
}
// You can now just pass your `fileArray` to this function as below
const files = await getNewFiles(fileArray)
console.log(files)
我想在对象数组中执行 promise,并在同一数组中用 promise 响应替换数组中的路径值,我正在尝试以下代码:
const cloudinary = require('cloudinary')
const cloudinaryConfig = require('../configs/cloudConfig.json')
cloudinary.config({
cloud_name: cloudinaryConfig.cloud_name,
api_key: cloudinaryConfig.api_key,
api_secret: cloudinaryConfig.api_secret
})
fileArray = [
{
fieldname: 'productThumbImage',
originalname: 'Boyka.png',
path: '/home/rahul/MaxDigiAssignment/mxNodeEcommerce/mx-ecommercenode/upload/product/1589983049420.png'
},
{
fieldname: 'productPhoto',
originalname: 'Code.png',
filename: '1589983049436.png',
path: '/home/rahul/MaxDigiAssignment/mxNodeEcommerce/mx-ecommercenode/upload/product/1589983049436.png'
},
{
fieldname: 'productPhoto',
originalname: 'Boyka.png',
filename: '1589983049438.png',
path: '/home/rahul/MaxDigiAssignment/mxNodeEcommerce/mx-ecommercenode/upload/product/1589983049438.png'
}
]
const files = imgs.map(async (img)=>{
let path = await cloudinary.v2.uploader.upload(img.path)
return {
...img,
filePath: path.url
}
})
console.log('files 11/// ',await files);
但我收到错误提示{ }
我想在每个对象中执行这个承诺然后得到响应 我预期的数组如下:
[
{
fieldname: 'productThumbImage',
originalname: 'Boyka.png',
path:'http://res.cloudinary.com/deqpxepbs/image/upload/v1589982424/ywfetodkvyadmdqjof1i.png'
},
{
fieldname: 'productPhoto',
originalname: 'Code.png',
filename: '1589983049436.png',
path:'http://res.cloudinary.com/deqpxepbs/image/upload/v1589982424/desb18dgungva5y8apbv.png'
},
{
fieldname: 'productPhoto',
originalname: 'Boyka.png',
filename: '1589983049438.png',
path:'http://res.cloudinary.com/deqpxepbs/image/upload/v1589982424/ugud4flh7gfl3ymyh0xx.png'
}
]
files
将是一系列承诺,而不是单个承诺。你应该:
console.log('files 11/// ',await Promise.all(files));
您想做的是:
- map
你的图像数组到 Promise
s 的数组
- await
你所有的 Promise
并将它们的结果作为 paths/URLs 的数组
- map
对象数组的路径数组。
const getNewFiles = async (imgs) => {
const promises = imgs.map(img => {
return cloudinary.v2.uploader.upload(img.path)
})
const paths = await Promise.all(promises)
/* If you're using Node.js version 12 or more recent, a better
alternative to `Promise.all` is `Promise.allSettled` */
// const paths = await Promise.allSettled(promises)
const newFiles = paths.map((path, index) => {
return {
...imgs[index],
path: path.url
}
})
return newFiles
}
// You can now just pass your `fileArray` to this function as below
const files = await getNewFiles(fileArray)
console.log(files)