question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error thrown in async write result in infinite loop

See original GitHub issue

Trying to debug this, it looks like the Promise wrapped test error is constantly alternating being thrown by core/useAtom.ts#L33 and some internal React code.

This results in constant Rendering Test messages being logged.

import { atom, Provider, useAtom } from 'jotai';
import { Suspense } from "react";
import { render } from "react-dom";
import { ErrorBoundary } from "react-error-boundary";

const testAtom = atom(undefined, (async () => {
  throw new Error('test');
}));

render(<App />, document.getElementById("app"));

function App() {
  return (
    <Provider>
      <ErrorBoundary fallbackRender={({ error }) => { return (<p>{error.toString()}</p>); }}>
        <Suspense fallback={"Loading..."}>
          <Test />
        </Suspense>
      </ErrorBoundary>
    </Provider>
  );
}

function Test() {
  console.log('Rendering Test');
  const [_, write] = useAtom(testAtom);

  return (
    <button onClick={() => write()}>Test</button>
  );
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
garrett-hoppercommented, Apr 10, 2021

@dai-shi, perhaps this should be a separate issue for discussion, but it seems like when a write function throws an error, it should be sent back to the caller so they can catch it and use something like useErrorHandler from react-error-boundary to display it to the user. It seems like at the moment the error is consumed and sent straight to the console.

0reactions
garrett-hoppercommented, Apr 10, 2021

Oh, duh, I wasn’t awaiting the returned promise. Nevermind; thanks for all your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to catch error promises in infinite while-loop with async ...
You can wrap your block in a try catch : while (true) { try { await promisifiedDelay(20000); users.map(async client => { //await ......
Read more >
plugin-transform-regenerator can produce infinite loop using ...
I'm using a library that uses finally blocks to handle cleanups inside async functions. Is there any way to clean this up at...
Read more >
Error handling with async/await and promises, n² ... - CatchJS
Async await error handling has a bunch of edge cases. ... Will not catch errors thrown in another call stack via a setTimeout()...
Read more >
JavaScript Mistake — Loops, Promises, and More
We don't want async functions as executors because when errors are thrown, they'll be lost and won't cause the newly constructed promise to ......
Read more >
Python behind the scenes #12: how async/await works in Python
The core of the program is the event loop – an infinite loop that on each iteration selects ready sockets and calls the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found