Home > Software engineering >  How to wait for a async function before run the other one
How to wait for a async function before run the other one

Time:11-27

there is 2 methods, I want to push data to database when the method getDownloadUrl finishes to push all urls to array

    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 = []
    },

CodePudding user response:

OP fixed the issue thanks to that answer.
So mainly using:

  • await Promise.all
  • async await
  • .map
  • and by removing .then
  • Related