Exit a while inside a function

Hello,
For the moment I am trying to realize a sponsorship system with wix.

For that I have 2 collections and 1 from wix :

  • n°1 : Contains a custom client id then all the referrals of this client
  • n°2 : Contains the information of the customer if we have to contact him
  • n°3 : The normal wix collection that keeps customer information and password

For that I am on a lightbox for the registration on the site. the code of this page is the following: - thanks to Velo-Ninja -

// ******************************** Import of libraries ********************************

import { authentication } from 'wix-members';
import wixData from 'wix-data';
import {memory} from 'wix-storage';

// ******************************** Declaration of global variables ********************************

memory.setItem("key", "wait");

// ******************************** Functions ********************************

// ---------- Checking the validity of the sponsor code ----------

function f_verifParrain(code) {

  // ===== Set up Filter ====
  let filter = wixData.filter();
  filter = filter.eq("idClient", code);

  // ===== Search in the database ====
  $w("#dataset1").setFilter(filter)
  .then(()=>{

    // ===== Retrieve the number of sponsors ====
    let total = Number($w('#dataset1').getTotalCount());
    console.log("Number of occurrences : " + total);

    // ===== Invalid code ====
    if (total !== 1 ){
      $w('#bInscription').disable();
      $w('#errorReco').show();
    }

    // ===== Valid code ====
    else {
      $w('#bInscription').enable();
      $w('#errorReco').hide();
    }
  }).catch((err)=>{console.log(err);})
}

// ---------- Client ID code generation ----------

async function f_setID(code) {
  
  let clientId = Number(code) + 1
  let loop = true

  while (loop === true) {

    // ===== Set up Filter ====
    let filter = wixData.filter();
    filter = filter.eq("idClient", clientId);

    // ===== Search in the database ====
    $w("#dataset2").setFilter(filter)
    .then(()=>{

      // ===== Retrieve the number of sponsors ====
      let total = Number($w('#dataset2').getTotalCount());
      console.log("Number of occurrences : " + total);

      // ===== Valid code ====
      if (total === 0 ){
        memory.removeItem("key");
        memory.setItem("key", clientId);
        console.log("clientId Valid: " + clientId);
        loop = false
        break;
      }

      // ===== Invalid code ====
      else {
        clientId = clientId + 1
        console.log("clientId : " + clientId);
      }
    }).catch((err)=>{console.log(err);})
  }
}

// ******************************** Validating the Recommendation Code ********************************

// ---------- Wait for the page to load ----------

$w.onReady(()=>{console.log('Page is ready');

// ---------- Waiting for Database 1 to load --------

  $w('#dataset1').onReady(()=>{
    console.log('Dataset1 ready');

    // ---------- Waiting for Database 2 to load --------

    $w('#dataset2').onReady(()=>{
      console.log('Dataset2 ready');

      // ---------- Detection of the filling of the recommendation field ----------

      $w('#iParrain').onChange(()=>{
        console.log("Code Changed");

        // ===== Function call ====
        let codeSponsor = $w("#iSponsor").value;
        console.log("code input " + codeSponsor)
        f_verifSponsor(codeSponsor);

        // ---------- Generation of the Customer ID code --------

        if( $w("#bInscription").enabled === true) {
          
          let key = memory.getItem("key");
          console.log("Key active " + key)

          // ===== Function call ====
          if (key === "wait"){
            let codeSponsor = $w("#iSponsor").value;
            f_setID(codeSponsor)
          }
        }

        // Send the 3 collections 

      });
    });
  });
});

The problem is that I get stuck in the loop while I can’t get out and break sends me an error