簡體   English   中英

有沒有比這更“漂亮”的方法來提取 [[PromiseResult]] ?

[英]Is there a "prettier" way to extract the [[PromiseResult]] than this?

我是新手,仍在學習如何使用 Firestore。 所以我設法console.log()我的 1 個文檔及其所有字段。 但是,我還沒有找到一種從 Firestore 中提取數據的好方法。 到目前為止,我設法使一切正常工作:

async function getProducts(specificProduct) {
  let data = await getDoc(doc(db, "MyProductList", specificProduct));

  return data.data();
}
let price = getProducts("someProduct");
let price1 = [];

function getDesc() {
  price
    .then((e) => {
      price1.push(e.Description);
    })
    .then(() => console.log(price1[0]));
}

但是,這對我來說不合適。 感覺很丑,雖然我不確定是不是我一個人。 所以我想知道是否有更好的方法來寫這個。

只需使用另一個async function 和await而不是then

async function getDesc() {
    const price = await getProducts("someProduct");
    const price1 = [];
    price1.push(price.Description);
    console.log(price1[0]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM