Async support for StateField create in CM6

I’m defining a new StateField which needs to hit an API endpoint to download some data to operate on. Right now it looks like StateField’s lifecycle methods are all synchronous, so I’m performing this fetch in the root of the application and passing that data to a StateField singleton function, which returns the StateField instance.

If possible, I would love to colocate this fetch with the StateField itself, so that I can move this logic out of the application root and package it alongside the field. Is there a recommended pattern for performing async work in StateField’s create handler?

The general pattern is to give the state field a special ‘pending’ value, and organize for the request to fire off an effect that gives it a proper value. If you need to keep mapping data from the document state at the time of the request to the current document when it finishes, the pending value could accumulate a ChangeDesc. Scheduling the actual requests is usually best done in a view plugin, though if there’s only a single request maybe that’s overkill.