smalljs$molSearch⌘ KDocsPlaygroundCompareEcosystemAboutEN
Data FetchingGetting StartedIntroductionMental modelGetting StartedFrom TypeScript to view.treeProject StructureToolingEssentialsInstallationViewsState & ReactivityRoutingRenderingTestingDeploymentTroubleshootingDataData FetchingData SchemasGiper BazaMoreShowcaseFrom React, Vue & SvelteCookbookAdvancedPluginsModule metadataOfflineGhost viewsAboutFAQTeamReleasesAPI$mol_button_major$mol_button_minor$mol_string$mol_number$mol_text$mol_paragraph$mol_list$mol_row$mol_link$mol_check$mol_switch$mol_select$mol_scroll$mol_page$mol_pick

Data Fetching

Loading remote data in $mol is not a special API — an async value is just a reactive property written as if the response were already there. The view waits for it, shows a loading state, and re-renders when the data arrives.

An async property

Call the network inside a @ $mol_mem and return the parsed result. There is no promise, no await and no callback in the code:1namespace $.$$ {2 export class $my_users extends $.$my_users {3 @ $mol_mem4 users() {5 return this.$.$mol_fetch.json( 'https://api.example.com/users' ) as {6 id: number7 name: string8 }[]9 }10 }11}$mol_fetch suspends the fiber until the response arrives, then the computation restarts and users() returns the array. While it is pending, any view that reads users() automatically shows the built-in loading state — you write no isLoading flag. Do not return a promise from a @ $mol_mem yourself: a promise stored as a value keeps the cell in the loading state forever, see Troubleshooting.this.$ is the component's context. Every service comes through it: $mol_fetch for the network, $mol_state_arg for the URL, $mol_after_timeout for time. In a test the context is replaced, so the same users() sends its request to a mock instead of the network, without a line of the component changing. Testing shows that mock.

Rendering the result

Bind the resolved data straight into a list:1 @ $mol_mem2 user_names() {3 return this.users().map( user => user.name )4 }When the response arrives, users() updates, user_names() recomputes, and the list renders. No callbacks, no useEffect.

Reloading

Because it is just a reactive cell, you refetch by invalidating it. Depend on a token you can bump:1 @ $mol_mem2 reload_token( next?: number ) {3 return next ?? 04 }5 6 @ $mol_mem7 users() {8 this.reload_token() // subscribe9 return this.$.$mol_fetch.json( 'https://api.example.com/users' ) as unknown[]10 }11 12 @ $mol_action13 reload() {14 this.reload_token( this.reload_token() + 1 )15 }
Edit this page on GitHubWas this helpful?YesNoPreviousTroubleshootingNextData Schemas
On this pageAn async propertyRendering the resultReloadingErrorsNext
Type to search the documentation.