Promises in Events are Fulfilled Automatically

Hello developers,

Promises in my datasets’ onReady() , onBeforeSave() and onAfterSave() are fulfilled once the page has finished loading, for example, I want to scroll to somewhere after the user submits a form, and update a database before submitting another form, but the page scrolls immediately once the page finish loading and update the databse immediately too, I did notice this because the “updated time” on the repeater’s element has changed.

Why is this happening? And how can I prevent it?

Notice the following page, the review update date will automatically change each and every time you reload the page, the on before and on after save functions are only checked when the user is logged in: Visit the page .

Help will be appreciated.

No answers yet! Just as expected :expressionless:

Have you read about promises: https://www.wix.com/corvid/forum/corvid-tips-and-updates/promises-promises

If you want to wait for a certain code to fire before firing the next use async/await . Something like this:

$w("#dataset1").onReady( () => {
   myFnc();
});

async function myFnc() {
   await console.log('First");
   await console.log('Second");
   console.log('Third");
}

Hi Shan,
Thanks for your reply. Yes I’m aware of the use of the async/await , and I’m using them along with the .then() , my problem is that some promises inside the onAfterSave() and the onBeforeSave() are executed automatically, for example, a box must show after save, and a scroll is should be done before save, but once the page loads, the box is shown and the page scrolled although non of their conditions were fulfilled.

I just checked the page you gave above, it did not scroll me to the review repeater like you said. See here: https://www.loom.com/share/f14a6005ed0841d5be7f42584beb9f0a

Hi shan, thanks for taking time to check out the issue.

These two functions are wrapped inside an if(user.loggedIn) , so it won’t scroll and won’t update the repeater unless the user is logged in.

@shantanukumar847

$w('#EditReviewDataset').onAfterSave(() => {
    if (wixUsers.currentUser.loggedIn === true) {
        $w('#readReviews').refresh().then(() => {
            $w('#editReviewStrip').collapse().then(() => {
                $w('#reviewsRepeater').scrollTo();
            })
        })
    }
});

Here’s a screen-cast:
https://www.screencast.com/t/T6fC9Bax
Pass key: RiskSadExposeChef