Hi i have function and want it works 3 times after i click button. Unfortunatelly it works once or when i write without “then” 3 times but values dont change because funcion goes too quick. Does anybody can take a look. Thanks
function dodajestol(){
$w(“#dataset1”).onReady( () => {
let count = $w(“#dataset1”).getTotalCount();
$w(“#dataset1”). new ()
.then(() => {
$w(“#dataset1”).setFieldValue(“miejsce”, count+101);
return $w(“#dataset1”).save();
})
.then(() => {
$w(“#dataset1”).refresh();
console.log(“New item saved”);
})
})}
export function button64_click(event) {
$w(‘#button64’).disable()
dodajestol()
.then(() => {
dodajestol()
})
.then(() => {
dodajestol()
})
.then(() => {
$w(‘#button64’).enable();
})
}
Maybe you should try this. I do not think the promises and updates.
export function button64_click(event) {
$w('#button64').disable()
runFunction(3);
$w('#button64').enable();
}
let counter = 0;
export function runFunction(times) {
if (counter < times) {
dodajestol()
.then(() => {
counter++;
runFunction(times)
});
}
}
I also think you are missing a return inside the returned promise like below.
function dodajestol(){
$w("#dataset1").onReady( () => {
let count = $w("#dataset1").getTotalCount();
$w("#dataset1").new()
.then(() => {
$w("#dataset1").setFieldValue("miejsce", count+101);
return $w("#dataset1").save();
})
.then(() => {
$w("#dataset1").refresh();
console.log("New item saved");
return; // this one
})
})}
If you don’t have that you will return before the promise has been resolved.
it gives me error in .then line
TypeError: Cannot read property ‘then’ of undefined
export function button64_click(event) {
$w(’ #button64 ‘).disable() runFunction(3);
$w(’ #button64 ').enable(); }
let counter = 0; export function runFunction(times) {
if (counter < times) { dodajestol()
.then(() =>
{ counter++; runFunction(times) }); } }