Breaking code in small blocks

Hi,

In this article https://support.wix.com/en/article/corvid-best-practices-for-building-a-corvid-website it is recommended to try breaking the code in small parts every time it is feasible.

If I understood this correctly, in the code below, it would be better to use LINE1&LINE2 where they are, than at the end of the code

Is this right ?. And if it is, is there a performance issue that does small code blocks work better than large ones ??

Best,


// CODIGO INCLUYE VISUALIZACIÓN DE USUARIO Y DE NOMCOM Y LUEGO ANTES DE REDIRIGIR A DINAMICA EL INSERT SEGÚN EL CIRCUITO SELECCIONADO
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import {session} from 'wix-storage';

let userEmail;  
let userId;
let flagcircuito;
let toInsert

$w.onReady(function () {

wixData.query("Members/PrivateMembersData")
  .eq("_id", wixUsers.currentUser.id)
  .find()
  .then( (results) => {
    userEmail = results.items[0].loginEmail;
    userId = results.items[0]._id;
 //$w('#text71').text = userEmail;
  });

 $w('#dynamicDataset').onReady( () => {
  $w('#dynamicDataset').getCurrentItem();

toInsert = {
 "title":        $w('#dynamicDataset').getCurrentItem().title, 
 "nomCom":       $w('#dynamicDataset').getCurrentItem().nomCom, 
 "codorig":      "ROJA", 
 "userEmail":    userEmail,
 "userId":       userId  
};
  wixData.insert("clubbitLogorig", toInsert) // INSERT COE (FIRST INSERT)

//  .then( (results) => {
}) // ------------------------------------------------------LINE 1
}) // ------------------------------------------------------LINE 2

$w("#repeater1").onItemReady(($w, dataItem, index) => {
    $w('#box71').onClick(() => {

  $w('#dataset1').getCurrentItem();

if (($w('#dataset1').getCurrentItem().codcirc === 1)) {

toInsert = {
 "title":        $w('#dataset1').getCurrentItem().title, 
 "nomcom":       $w('#dataset1').getCurrentItem().nomcom, 
 "codorig":      "ROJA", 
 "codcirc":       1, 
 "userEmail":     userEmail,
 "userId":        userId, 
 "nodocircid":    $w('#dataset1').getCurrentItem()._id, 

};

  wixData.insert("clubbitLogcirc", toInsert) // (SECOND INSERT)
    session.setItem("flagcircuito", $w('#dataset1').getCurrentItem().nomcirc);

 let linkToDynamicPage = dataItem["link-clubbitnodo-1-title"];
    wixLocation.to(linkToDynamicPage)

}

***************************
// code continues 

It depends how to break the code, where to break the code and when to break the code. Sometimes a big block is even better.

Hey man, again thanks for replying

So the problem would be that it’s up to me to decide the how/where/when

I asked that not only because of a performance issue, but because I was having a hard time with my code in relation with “the when” was my data available for an insert to fully work…

Some queried data was empty in an insert when onready, but in a later onclick event the data was correctly inserted in a different collection.

So after trying using return and fail, I changed the first part of my code and used (for the first time) async/await and fortunately it worked (!)

Anyhow, I still have a bunch of doubts in relation with data availability from queries, dynamicdataset or dataset, as I understand they have different response speed.

Cheers,

Yes, it is not really the size of a code-block which makes you this issues.
It is the lack of knowledge how&when to use (.then(()=>{ }) and/or how to use Async/Await.

You can use both of this methods. But on my opinion → Async-Await is more flexible, and you can use it almost every-where and every-time, when you need it.

Normal-function:

function myFunction(){    } 

Async-Await-function:

async function myFunction(){ let x = await xxxxxxxxxx  } 

Use it always then, when you have to wait for DATA.
In most cases these are LOADING and SAVING-Processes, which needs time.

A normal function will throw you ERRORs, because the code continues, without looking (if DATA already is —> READY).

This will not happen when you use Async-Await!