0

I try to receive from a helperUtils file, the value that i get in a function. In this function first i call to backend to get some information (this works perfect), then with this value (is an array of data) i do somethings inside te function to return a new value.

When i call this function from my .jsx file and try to receive the final value, i receive Promise in my console when i do a console.log.

function inside Helperutils.js

export const getResponseId = async (idStart) => { 
  const res = await getResponsIdApiNew(idStart);  
  if (res.length === 0) {    
    return (idStart + "1");
  } else {    
    let numbersArray = [];
    for (let j = 0; j < res.length; j++) {      
      if (res[j].substring(idStart.length, res[j].length)[0] !== "") {            
        numbersArray.push(Number(res[j].slice(idStart.length, res[j].length)));           
      }
    }
    if (numbersArray.length !== 0) {         
      const max = Math.max(...numbersArray);              
      let exist = false;
      for (let i = 1; i <= max; i++) {           
        if (!res.includes(idStart + i)) {          
          exist = true;
          idStart = idStart + i;
          console.log(idStart); //give me the value that i want
          break;
        }
      }
      if (exist) {
        console.log(idStart); //give me the value that i want
        return (idStart);
      } else {
        let number = max + 1;
        console.log(idStart + number)     
        return (idStart + number);
      }
    }
  }  
};

in my file.jsx:

i did try:

const getNewIdResponse = async () => {
    await getResponseId(`${getTopic}_${typeResponseTitle}_`);
  };
  console.log(getNewIdResponse);

and try:

const getNewIdResponse = getResponseId(`${getTopic}_${typeResponseTitle}_`);
console.log(getNewIdResponse);

all consoles print:

enter image description here

ddaudiosolutions
  • 131
  • 2
  • 15
  • 1
    The async function you tried: (a) the async function doesn’t return anything, and (b) you log the function reference, not the results of calling the function. The last code snippet: you’re not awaiting. I might take a quick step back and find a decent async programming tutorial/blog/etc; it’ll be more helpful in the long run. – Dave Newton Jun 29 '22 at 11:59
  • well because i am using this logical in a reactproject. For this reason i puted the tag 'reactjs'. – ddaudiosolutions Jun 29 '22 at 12:14

0 Answers0