Search.../

onReadyAsync( )

The onReadyAsync() function returns a Promise that resolves when a dataset is ready.

Description

Similar to the onReady() function, the onReadyAsync() function allows you to optionally perform actions once a dataset is ready. A dataset is ready after it has loaded its data from the collection and updated all connected page elements with their corresponding values. onReadyAsync() differs from onReady() in that onReadyAsync() is promise-based, whereas onReady() is based on callback functions.

Syntax

function onReadyAsync(): Promise<void>

onReadyAsync Parameters

This function does not take any parameters.

Returns

Fulfilled - When the dataset is loaded.

Return Type:

Promise<void>

Related Content:

Was this helpful?

Get the dataset's current item after the dataset is ready

Copy Code
1$w.onReady(async () => {
2 await $w('#myDataset').onReadyAsync();
3 let currentItem = $w('#myDataset').getCurrentItem();
4 console.log(currentItem);
5} );
6
7/*
8 * {
9 * "_id": "fcec780a-3e37-4b64-8a66-37c552c53f99",
10 * "_owner": "f6c0f9c3-a62d-7e9f-g58d-943829af244d9",
11 * "_createdDate": "2017-05-01T17:19:03.823Z",
12 * "_updatedDate": "2017-05-01T17:19:10.477Z",
13 * "title": "Dr. ",
14 * "name": "John",
15 * "age": "50"
16 * }
17 */
Get and add the total number of items from multiple datasets after they are ready

This example gets the number of items from multiple datasets. It uses the Promise.all() function, which takes an array of promises returned by 2 onReadyAsync() functions, and returns them as a single Promise. This Promise is only resolved once both onReadyAsync promises are are resolved. Once fulfilled, the total number of items from each dataset is retrieved.

Copy Code
1$w.onReady(async () => {
2 const promise1 = $w('#myDataset1').onReadyAsync();
3 const promise2 = $w('#myDataset2').onReadyAsync();
4
5 await Promise.all([promise1, promise2]);
6 const combinedTotal = $w('#myDataset1').getTotalCount() + $w('#myDataset2').getTotalCount();
7 console.log(`Combined total items in both datasets: ${combinedTotal}`); //8
8} );
Get the total number of items after the dataset is ready

This example demonstrates how both onReady() and onReadyAsync() functions do the same thing.

Copy Code
1$w.onReady(async () => {
2 //onReady
3 await $w("#myDataset").onReady( () => {
4 let onReadyTotalCount = $w("#myDataset").getTotalCount(); //4
5 } );
6
7 //onReadyAsync
8 await $w("#myDataset").onReadyAsync();
9 let onReadyAsyncTotalCount = $w("#myDataset").getTotalCount(); //4
10} );
11