在内部更新。在javaScript中不反映在外面

发布于 2025-02-08 09:46:21 字数 498 浏览 2 评论 0原文

我尝试在“ fetch”中更新内部“。 - 执行put-in-idbobjectStore-the-transaction-has-has-has-has fined'>后链接。更新“。然后”之外的索引edDB数据。 因此,我创建了一个布尔变量,并尝试在“。然后”内部更新它,并考虑过在外部更新索引的数据,但是布尔值未在“。然后”内部更新。

.then(() =>{
        data_inserted = true ;
      })

Now outside the ".then"

console.log(data_inserted); // value is false
      if ( data_inserted === true )
      {
// update IndexedDB code
      }

I saw this post

请帮助我更新布尔变量。

提前致谢。

I tried updating IndexedDB data inside ".then" in fetch but that doesn't work inside event handlers as I got to know that from this post post-link. Updating the IndexedDB data outside the ".then" is working.
So I created a boolean variable and tried updating it inside the ".then" and thought of updating the IndexedDB data outside but the boolean value is not getting updated inside the ".then".

.then(() =>{
        data_inserted = true ;
      })

Now outside the ".then"

console.log(data_inserted); // value is false
      if ( data_inserted === true )
      {
// update IndexedDB code
      }

I saw this post post-link and I'm not sure how to do a callback function as they did for my code.

Kindly help me in updating the boolean variable.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2025-02-15 09:46:21

我怀疑这缺乏对异步代码的了解。

异步代码,将在跟随它的代码之后发生。注意:

let foo = "bar"
someFunctionThatTakesOneSecond()
    .then((res) => {
        foo = "baz"
        console.log("then: ", foo); 
    })
console.log("there: ", foo)

会输出:

 there: bar
 then: baz

为什么?因为“ then”中的代码直到``somefuncunthattakeseonecond''完成并实现了诺言。但是,在异步块之后的代码将同步运行(即马上:马上)。

您可能希望使用异步/等待/等待 - 等待停止执行,直到异步函数

模式

let foo = "bar"
await someFunctionThatTakesOneSecond()
foo = "baz"
console.log("then: ", foo); 
console.log("there: ", foo)

返回

 then: baz
 there: baz

。 /placeel-assynchronous-javascript/“ rel =“ nofollow noreferrer”> https://blog.logrocket.com/understanding-asynchronous-javascript/

I suspect this is a lack of understanding of async code.

The async code, then, will happen AFTER the code that follows it. Note:

let foo = "bar"
someFunctionThatTakesOneSecond()
    .then((res) => {
        foo = "baz"
        console.log("then: ", foo); 
    })
console.log("there: ", foo)

Will output:

 there: bar
 then: baz

Why? Because the code in the 'then' won't run until after `someFunctionThatTakesOneSecond" completes and the promise is fulfilled. But the code after the async block will run synchronously (ie: right away).

You may wish to use the async/await pattern instead - await stops further execution until the async function returns.

So:

let foo = "bar"
await someFunctionThatTakesOneSecond()
foo = "baz"
console.log("then: ", foo); 
console.log("there: ", foo)

Would output:

 then: baz
 there: baz

Read more on async/await and asynchronous JavaScript here: https://blog.logrocket.com/understanding-asynchronous-javascript/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文