如何在运行另一个异步函数之前等待异步函数

问题描述 投票:0回答:1

有2个方法,我想在方法getDownloadUrl完成将所有url推送到数组时将数据推送到数据库

    async getDownloadUrl(newCardData) {
      const cardImagesRef = await this.$fire.storage
        .ref('/albums_cards/')
        .child(uuidV1())
      this.newCard.cardImages.forEach(async (image) => {
        const imageRef = cardImagesRef.child(uuidV4())
        await imageRef.put(image)
        const url = await imageRef.getDownloadURL()
        newCardData.images.push(url)
      })
    },

    async postNewCard() {
      this.newCard.loading = true

      const newCardData = {
        title: this.newCard.cardTitle,
        description: this.newCard.cardSnippet,
        images: [],
      }

      await this.getDownloadUrl(newCardData)
      await this.$fire.database.ref('albums/cards').push(newCardData)
      this.newCard.loading = false
      this.newCard.cardTitle = null
      this.newCard.cardSnippet = null
      this.newCard.cardImages = []
      this.newCard.cardImageUrls = []
    },
firebase vue.js firebase-realtime-database vuejs2 nuxt.js
1个回答
1
投票

OP 修复了问题,感谢 那个答案
所以主要使用:

  • await Promise.all
  • async + await
  • .map
  • 并删除
    .then
© www.soinside.com 2019 - 2024. All rights reserved.