【问题标题】:calling an api inside map function In node Js在节点 Js 中调用 map 函数中的 api
【发布时间】:2023-02-09 14:39:11
【问题描述】:
const processInboundEmailAttachment = async (files: any[]): Promise<any[]> => {

  const attachments = []

  await Promise.all(
    files.map(async (file) => {
      try {

        let response = null

        response = await axios.get(file.url, {
          headers: {
            Accept: "message/rfc2822"
          },
          auth: {
            username: "api",
            password: "api-key"
          },
          responseType: "stream"
        })

        if (response && response.data) {

          response.data.pipe(concat(async data => {
            try {
              const mediaUrl = await uploadToFirestore(file["content-type"], data, file.name)

              attachments.push({
                name: file.name,
                url: mediaUrl,
                id : uuidv4()
              })

            } catch (error) {
              console.log("err", error);
            }

          }));

        }

      } catch (err) {
        log.error(err)
      }
    })
  )

  return attachments // from here this return initial attachments [] array

}

 const uploadAttachment = async () => {
 const attachment =  await processInboundEmailAttachment(JSON.parse(attach))

 console.log("attachment",attachment);
 // I want updated pushed attachment array here but I got [] initial decalare value 
}

app.get("/uploadAttachment", uploadAttachment)

在附件控制台日志中我得到了 [] 数组,它返回数组的初始赋值。 它不是等待 API 响应和新推送的数组。

我认为 Promise 中存在一些问题,不是等待更新数组,而是 直接返回初始附件数组

提前感谢您的帮助

【问题讨论】:

  • 实际产量:- [ ]预期产出:- [{name,url,id}{name,url,id}....]
  • 什么是concat

标签: javascript node.js asynchronous async-await es6-promise


【解决方案1】:

看起来它不是在等待 uploadFirestore 响应。您可以像下面这样减少您的功能并将其包装为自定义promise

const processInboundEmailAttachment = async (files: any[]): Promise<any[]> => {
  const getFileUploadedResult = function(file) {
    return new Promise(async (resolve, reject) => {
      try {
        let response = null
        response = await axios.get(file.url, {
          headers: {
            Accept: "message/rfc2822"
          },
          auth: {
            username: "api",
            password: "api-key"
          },
          responseType: "stream"
        })
        if (response && response.data) {
          response.data.pipe(concat(async data => {
            try {
              const mediaUrl = await uploadToFirestore(file["content-type"], data, file.name)
              resolve({
                name: file.name,
                url: mediaUrl,
                id : uuidv4()
              })
            } catch (error) {
              reject(err)
            }
          }));
        }
      } catch (err) {
        log.error(err)
        reject(err)
      }
    })
  }
 return Promise.all(
    files.map(async (file) => {
      return getFileUploadedResult(file)
    })
  )
}

 const uploadAttachment = async () => {
 const attachment =  await processInboundEmailAttachment(JSON.parse(attach))

 console.log("attachment",attachment);
 // I want updated pushed attachment array here but I got [] initial decalare value 
}

app.get("/uploadAttachment", uploadAttachment)

【讨论】:

  • 避开Promise constructor antipattern!您可能需要 new Promise 在这里,但它仍然应该只用于承诺使用异步回调的最小部分。
  • @Bergi 你的意思是不是使用 Promise.all 用 for...of 和 async await 来代替它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-21
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 2018-11-21
  • 2021-09-27
相关资源
最近更新 更多