Waiting for a download within an object

Hi all,

I have a notebook in which data are downloaded into an object:

https://beta.observablehq.com/@sdwfrost/distance-calculation-with-tn93-js

The line involved is this:

m = msa.default({importURL: url})

This returns an object immediately, which is subsequently updated when the data loads. The problem is that dependent cells try to run immediately, rather than wait for the object to be updated. How can I get a cell to depend on a change in another cell?

This is related to this query:

https://talk.observablehq.com/t/does-mutable-work-with-objects/399

Sure! For something that’s asynchronous like this, the msa module needs to give a signal back to the rest of the program, whether that’s calling a callback, emitting an event, or returning a Promise. One way to do it is:

m = new Promise(resolve => {
  let m = msa.default();
  m.importURL(url, () => resolve(m));
})

Unlike the importURLconfiguration option at start, using msa’s importURL method lets you optionally specify a callback to be called once the data is imported, and I use that to wait for the value.

Another option is using the MSA module’s import:url event:

m = new Promise(resolve => {
  let m = msa.default({importURL: url});
  m.g.on('import:url', () => resolve(m));
})

Documentation for this part of MSA is pretty scarce, so I dig into the snippets directory and read the source to figure out what’s going on - there are a handful of examples that do async.