簡體   English   中英

不應該執行的承諾

[英]Promises being executed when they shouldn't be

我有幾個數據庫突變,我想一次全部執行,而不是同步執行。 我遇到的問題是,當我嘗試將這些承諾推入數組時,它們會執行。

我在這里做錯了什么? 我也試過推送匿名函數,像這樣,

promises.push(
    async () => await someDbMutation1({ someForeignKey: "10" }),
)

但它們不會在Promise.all期間執行。

自信-奧斯汀-sifop

import * as React from "react";
import "./styles.css";

const someDbMutation1 = async ({ someForeignKey }) => {
  return await new Promise((resolve) => {
    console.log("should not enter");
    return setTimeout(() => {
      resolve("aa");
    }, 2000);
  });
};

const someDbMutation2 = async ({ someParameter }) =>
  await new Promise((resolve) =>
    setTimeout(() => {
      resolve();
    }, 2000)
  );

export default function App() {
  const [loaded, setLoaded] = React.useState(false);

  React.useEffect(() => {
    init();
  }, []);

  const init = React.useCallback(async () => {
    const promises = [
      someDbMutation1({ someForeignKey: "10" }),
      someDbMutation2({ someParameter: "abc" })
    ];

    // await Promise.all(promises);

    setLoaded(true);
  }, []);

  return <div className="App">{loaded && <div>done</div>}</div>;
}

我希望在調用Promise.all期間執行promises數組中的這些承諾,但顯然情況並非如此。 我最近才注意到這一點,當我將null作為外鍵的值傳遞時,此時我的數據庫中的鍵約束將其拾取並引發錯誤。

現在我很擔心,因為我經常使用promises數組並遍歷 db 對象並將突變查詢push送到promises中——這意味着每個請求都會執行兩次。 我不確定我在這里缺少什么。

對於您說它沒有執行的問題的第一部分:

promises.push(
    async () => await someDbMutation1({ someForeignKey: "10" }),
)

這是因為您推送的是匿名 function而不是promise - 它們是兩個不同的東西。 根據您的數組名稱,我認為預期的行為將是您這樣做:

promises.push(
    someDbMutation1({ someForeignKey: "10" })
)

如果您希望在一個時間點執行所有承諾,那么您可以這樣做:

queries.push(
    async () => await someDbMutation1({ someForeignKey: "10" }),
)

/ ** -- some point later -- ** /

const promises = queries.map(q => q()) // Execute queries
const results = await Promise.all(promises) // Wait for queries to finish

此外,您對Promise.all的工作原理有誤解:

我希望在調用 Promise.all 期間執行 promises 數組中的這些承諾

Promise.all執行承諾,它等待承諾解決。 這里有一個參考

所以在這一部分:

const promises = [
  someDbMutation1({ someForeignKey: "10" }),
  someDbMutation2({ someParameter: "abc" })
];

您實際上是在執行這些函數,因此如果您要console.log的 promises 數組,它看起來像這樣:

[
  Promise (unresolved),
  Promise (unresolved)
];

然后在await Promise.all()之后, promises數組將如下所示:

[
  Promise (resolved: value),
  Promise (resolved: value)
];

問題 1:必須在實際等待它們的塊中等待 Promise。

const someDbMutation1 = async ({ someForeignKey }) => {
  return new Promise((resolve) => {
    console.log("should not enter");
    return setTimeout(() => {
      resolve("aa");
    }, 2000);
  });
};

const someDbMutation2 = async ({ someParameter }) =>
  await new Promise((resolve) =>
    setTimeout(() => {
      resolve();
    }, 2000)
  );

問題是你正在執行承諾。 您應該將它們作為匿名函數添加到數組中,該函數使用您想要的參數調用您的 function。 所以這應該是這樣的。

const init = React.useCallback(async () => {
    const promises = [
      async () => someDbMutation1({ someForeignKey: "10" }),
      async () => someDbMutation2({ someParameter: "abc" })
    ];

    await Promise.all(promises);

    setLoaded(true);
  }, []);

我希望這是您正在尋找的答案。

暫無
暫無

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

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