Say you have something like: ```ts async function applyMigrations(storage: DurableObjectStorage) {
Say you have something like:
In DO constructor:
There are a few issues with this code:
In DO constructor:
There are a few issues with this code:
- Even if
storage.get/storage.set's implementations are sync, becauseapplyMigrationis async,.thenputs continuation in the next event loop, so the log order will be
->beforeafter->initialized. I haven't tested it but unless DO has a different behavior to regular JS, that should be the case. - Because continuation goes on the next event loop, the DO is not fully initialized (
this.someStateis still
) by the end of current event loop. I don't know if DO is able to process messages in the same event loop as the one doing the construction, but if that's true, then that's an issue.undefined - Because initialization is in continuation, if you are using TS then you must null assert `someState!` because as far as TS can see, `this.someState` is not initialized when constructor exits.To fix some of these issues, you have to instead do:```tsclass MyDO extends DurableObject { someState!: number constructor(ctx: DurableObjectState, env: Env) { super(ctx, env) ctx.blockConcurrencyWhile(async () => { await applyMigrations(ctx.storage) this.someState = 42 }) }}```(This however still doesn't fix the last issue and you still have to `someState!`)If a sync KV API is exposed, this entire problem no longer exists.
