Return object to the output in JS

Hi There,

I would like to ask the help of the community in the matter below. I’ve created a javascript which iterate through an array list. The async function return a list of object which is show the correct respond in the promise.


image

My problem is I can’t pass the return object to the output from here:
image
The console says:
image
I guess the return cannot reach the “ertek” output value so it cannot pass.

I’m not familiar so much with JS but I started to learn some so I can write the correct script but honestly I’m stuck here since a few days. Since then browsing sample codes, trying to learn better JS but I don’t know what is the problem. I guess it’s not a big deal but I’m not capable of fix it yet.

Here is the full code:

const values = inputs.datatable;
const rawg_key = inputs.key;
const finalarray = [];

async function fetch_data() {
  const rawg_key = "(censored)";

for (let i = 0; i < values.length; i++) {
    let call_url = "";
    let result_url = call_url.concat("https://api.rawg.io/api/stores/", values[i].store_id, "?key=", rawg_key);

    let response = await fetch(result_url, {
      method: "GET",
    });

    if (!response.ok) {
      var error_detail = `An error has occured: ${response.status}`;
      throw new Error(error_detail);
    }

    var data = await response.json();
    // await b.push(data);
    //b.push(data);
    finalarray.push({"name": data.name, "id": values[i].store_id, "image_url": values[i].url});
  }

  console.log(finalarray);
  return finalarray;
}

fetch_data()
  .then(data => {

let normalJSON = JSON.stringify(data);
console.log(normalJSON)
return { normalJSON }; //Here is the problem - 

  })
  .catch(err => console.error(err));



console.log(normalJSON) returns correctly:
image

If I add the ‘return { finalarray }’ to the main stack it return empty array.

I would really appreciate any kind of advice which could leads me to the solution.

Thanks in advance!

Okay, I found the solution. Here is the code:

const values = inputs.datatable;
const rawg_key = inputs.key;
const finalarray = [];

try {

for (let i = 0; i < values.length; i++) {
    let call_url = "";
    let result_url = call_url.concat("https://api.rawg.io/api/stores/", values[i].store_id, "?key=", rawg_key);
    const response = await fetch(result_url);
    const json = await response.json();
    console.log(json);
    finalarray.push({store_id: json.id, name: json.name, url: json.domain});

}
    return [0, { ertek: finalarray }]
} catch (err) {
  const error = {
    code: 'unknownError',
    message: 'Something went wrong.',
    rawError: err,
  }
}

Now it gives me the correct list of objects:
image

I could set as variable to list of items:

I’m so reliefed. The solution was front of my eyes (Async/await section):