Why do you need to split the code? The code block passed into Scrivito.load needs to be “pure” in the sense that it must not have any side effects. This is because the code may be executed multiple times by Scrivito.load, so changing state while performing I/O would be a bad idea. You can also think of it like this: The code inside Scrivito.load should only read data, not write or modify anything.

Any code with side effects (like changing state, interacting with the browser, performing network requests) needs to be run asynchronously, i.e. using the then method of the Promise returned by Scrivito.load.

Keep in mind that Scrivito.load is not needed for rendering. When rendering, it's much easier to use Scrivito.connect (see above).