如何在另一个承诺中返回承诺的价值?

时间:2019-04-27 21:58:17

标签: angular typescript firebase google-cloud-firestore ionic4

我试图在一个Promise中返回一个Promise函数的值。诺言仍然很新,因此请多多指教!

// card.service.ts

getCard(cardId: string): DocumentData {
    return firebase.firestore().collection('users').doc(this.currentUserId)
        .collection('cardsList').doc(cardId)
        .get()
        .then((docSnapShot) => {
            console.log((docSnapShot));
            return docSnapShot.data();
        },
        (err) => {
            console.log(err);
        });
  }

addCardToWallet(userId: string, cardId: string, dataObject: DocumentData) {
      return firebase.firestore().collection('users').doc(userId)
          .collection('walletList')
          .doc(cardId)
          .set(
              dataObject
          );
  }

// scanner.component.ts
scanCode() {
   this.barcodeScanner.scan(
          {
           showFlipCameraButton : true, // iOS and Android
           showTorchButton : true, // iOS and Android
           prompt : 'Scan a Kardbox QR' // Android
          }
     ).then((barcodeData) => {
         this.cardService.getCard(barcodeData.text)
             .then((data) => {
               // I want to add this to the database first

               this.cardService.addCardToWallet(this.currentUserId, barcodeData.text, data);

              // After adding values to database, navigate to other page

              this.router.navigate(['/home/wallet']);
        });
     });
 }

我得到的输出是条形码数据的值,我不知道为什么这样做。

3 个答案:

答案 0 :(得分:0)

我认为您应该尝试异步等待,请替换:

this.barcodeScanner.scan(
        {
          showFlipCameraButton : true, // iOS and Android
          showTorchButton : true, // iOS and Android
          prompt : 'Scan a Kardbox QR' // Android
        }
    ).then((barcodeData) => {
        this.cardService.getCard(barcodeData.text)
            .then((data) => {
              // I want to add this to the database first
          this.cardService.addCardToWallet(this.currentUserId, barcodeData.text, data);

          // After adding values to database, navigate to other page

          this.router.navigate(['/home/wallet']);
    });
});

具有:

const barcodeData = await this.barcodeScanner.scan(
  {
    showFlipCameraButton : true, // iOS and Android
    showTorchButton : true, // iOS and Android
    prompt : 'Scan a Kardbox QR' // Android
  },
);
const data = await this.cardService.getCard(barcodeData.text);

this.cardService.addCardToWallet(this.currentUserId, barcodeData.text, data);
this.router.navigate(['/home/wallet']);

别忘了在scancode()方法之前添加异步。

答案 1 :(得分:0)

您应该在getCard(cardId:string)函数中返回一个诺言

getCard(cardId: string): Promise<DataType or any> {
    const promise = new Promise((resolve,reject) => { 
         firebase.firestore().collection('users').doc(this.currentUserId)
        .collection('cardsList').doc(cardId)
        .get()
        .then((docSnapShot) => {
            console.log((docSnapShot));
            resolve(docSnapShot.data());
        },
        (err) => {
            console.log(err);
        });
    });
    return promise;
  }

如果要在将数据添加到数据库后进行导航,则应将router.navigate放入addCardToWallet方法的回调中

this.cardService.addCardToWallet(this.currentUserId, barcodeData.text,data).then(res => {
      router.navigate(/somwhere)
});

答案 2 :(得分:0)

也许这会有所帮助。请告诉我是否可以。

scanCode( ) {
    this.barcodeScanner.scan({
        showFlipCameraButton: true,
        showTorchButton: true,
        prompt: 'Scan a Kardbox QR'
    })
    .then( ( barcodeData ) => {
        this.cardService.getCard( barcodeData.text )
            .then( ( data ) => {
               // addCardToWallet returns a promise, right? Wait for it to return
               this.cardService.addCardToWallet( this.currentUserId, barcodeData.text, data )
                   .then( ( ) => {
                       // And after the card is added, tell the router to navigate
                       this.router.navigate([ '/home/wallet' ]);
                   })
            });
     })
     // Just in case, I recommend you to catch any error that might arise in the promise chain
     .catch( ( error ) => {
         console.error( 'Oops, something failed: "' + error + '"' );
     });
 }