Skip to content

Asynchronous Coding

Ashley Davis edited this page Oct 30, 2023 · 1 revision
  • Data-Forge Notebook (DFN) supports evaluation of asynchronous code in JavaScript and TypeScript.
  • Each code cell is automatically wrapped in an asynchronous function. This means you can use the await keyword directly in any code cell.
  • When a code cell starts executing it is placed in the evaluating state. This is indicated in the notebook editor with an animated busy indicator in the middle of the cell.
  • A code cell remains in the evaluating state until all its asynchronous operations have terminated.
  • When a notebook starts executing it is placed in the evaluating state. This is indicated by the animated busy indicator in the top right of the notebook editor GUI.
  • The notebook is kept in the evaluating state until all async operations across all code cells have completed.
  • Top level code in code cells runs in sequence across the notebook. This next cell doesn't begin execution until the previous has completed. Using the await keyword in a cell causes execution of the next cell to be delayed until that asynchronous operation has completed.
  • A code cell may start asynchronous operations without using the await keyword, in that case the next code cell will begin execution before the async operations from the previous have completed. When this happens multiple code cells will have async operations running in parallel. This is indicated by busy indicators on multiple cells.
  • It is possible to hang notebook evaluation by creating an endless async operation (e.g. a recursive call to setTimeout). DFN has no automated way to detect this, but it's always possible for you to manually stop evaluation by clicking the stop button.
  • Sometimes you might have to write code that keeps async operations alive across multiple cells. For example, opening a connection to MongoDB and then closing it several cells later. There is no problem with this kind of code when you are evaluating the entire notebook (Ctrl+Alt+R). But if you only do a partial evaluation (Ctrl+R) and evaluation stops before the async operation is wound up (e.g. closing the connection to MongoDB) this will hang the notebook. Again DFN has no automatic way to detect this situation. If it happens you'll have to stop evaluation manually by clicking the stop button.