【问题标题】:Promise is in pending while in each array of object在每个对象数组中,Promise 处于待处理状态
【发布时间】:2020-09-06 22:39:44
【问题描述】:

我想在对象数组中执行承诺,并用承诺响应替换数组中的路径值,我在下面的代码中尝试过:

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'
  }
]

【问题讨论】:

    标签: javascript node.js callback es6-promise cloudinary


    【解决方案1】:

    files 将是一组承诺,它不是一个承诺。你应该:

    console.log('files 11///  ',await Promise.all(files));
    

    【讨论】:

      【解决方案2】:

      你想做的是: - map你的图像数组到Promises的数组 - await 你所有的Promises 并以路径/URL 数组的形式获取它们的结果 - 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)
      

      【讨论】:

        猜你喜欢
        • 2014-06-21
        • 2018-03-31
        • 1970-01-01
        • 2016-07-17
        • 2021-10-12
        • 2015-02-19
        • 2021-10-07
        • 2014-09-20
        • 1970-01-01
        相关资源
        最近更新 更多