Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
return variables from arrow function [JS]

I have this function getData which will read in a list of IDs from a JSON file. the list of IDs is stored to the idsList variable which i need to use inside another function. How do i return the value from this function

function getData(){
    fetch("JSONitemIDsList.json")
    .then(response => response.json())
    .then(data => {
        let idsList = data.ids 
        //return idsList      
    })  
}

function myFunc(idsList) {
     //do something with idsList
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can approach this in many ways. For example:

// with async / await
async function getData() {
    try {
        const rawData = await fetch("JSONitemIDsList.json")
        const data = await rawData.json()
        return data
    } catch (err) {
        // handle error here
        console.log(err)
    }
}

async function myFunc() {
    const idsList = await getData()
     //do something with idsList
}

or

// define first
function myFunc(idsList) {
     //do something with idsList
}

function getData(){
    fetch("JSONitemIDsList.json")
    .then(response => response.json())
    .then(data => {
        // function call
        myFunc(data.ids)  
    })  
}
about 4 years ago · Juan Pablo Isaza Report

0

You can try this:

const reqResponse = fetch("JSONitemIDsList.json")
    .then(response => response.json())  
    // I am not sure, if your data is already in the format that you need, otherwise, you can run one more .then() to format it

}

const getIdList= async () => {
const idList = await reqResponse;
console.log(idList)
// use idList to access the information you need

}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!