When I call this function from a different cell and assign it to a variable, this results in an empty array: summaries = getSummaries()
// summaries = Array()[0]
However, if I expand the array by clicking on it (e.g., on the triangle), it suddenly gets populated with three items.
The same behaviour is also the case when I call Inputs.table(summaries). It starts with no results, but if the cell is re-run, it is populated with three items.
So, although I can manually fix the issue, what I would rather like is these values to be loaded when the notebook initializes.
I suspect the issue has to do with my poor knowledge of promises and not handling them correctly in the map loop. Indeed, the problem does not occur when I make the queries one by one.
I isolated the behavior by reproducing a minimal version of the code in this notebook.
I made quite a few searches on this form and on StackOverflow to no avail, and I would appreciate any advice you can provide. Thank you very much in advance!
getSummaries() returns a reference to an empty array
The cell where you view the arrayâs content only runs once, when the array is still empty. Observableâs Inspector lazily creates an HTML representation of the top level of this empty array.
Internally the function populates the array, but doesnât inform the Observable Runtime about the changes.
When you expand the array, the Inspector looks into the array (to which it still has a reference) to fetch the next level of properties and render them. But the array has changed in the meantime.
When a cell returns a promise, Observableâs Runtime wonât run dependent cells until the promise has resolved. The value that is returned by the promise gets passed on to other cells.
It works like a charm!
Indeed, not much use in making a promise if youâre not going to return it
Thanks a million for the fix, the more elegant way of fetching, and for the explanation of the logic.
Much appreciated!
Thank you Mike! The notebook I linked is a simplified version of the original code. In the original, there are some arguments being passed and a wish to push the complexity to the Appendix, hence the function. Good to know your preference on these sorts of cases, though!