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

145
Views
Changing a declared var inside a function

I need help with my script. Basicly my problem is that i have a variable declared with null, then inside a function changed to some value and then returned. But when i return it it still returns as null, not the thing i set it to. Anyone here to help?

Script:

var grade=""
var gradeStyle=""
var finalColor=""
function getSettings(){
    chrome.storage.sync.get("grade",function(res){
        grade=res.grade
    })
    chrome.storage.sync.get("gradeStyle",function(res){
        gradeStyle=res.gradeStyle
    })
    chrome.storage.sync.get("finalColor",function(res){
        finalColor=res.finalColor
    })
    return [grade,gradeStyle,finalColor]
}
export {getSettings};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The values are null because you are assigning these variables inside a callback function before returning them. It means that, the return values will not be assigned to variables until the promises completely resolves. To solve that, you can use the async-await syntax.

async function getSettings(){
    const grade = await chrome.storage.sync.get("grade").promise();
    const gradeStyle = await chrome.storage.sync.get("gradeStyle").promise();
    const finalColor = await chrome.storage.sync.get("finalColor").promise();
    return [grade,gradeStyle,finalColor]
}
export {getSettings};

Note that, to use the getSettings function now, you have to call it inside another async function, and you should await for its returning results.

about 4 years ago · Juan Pablo Isaza Report

0

EDIT:

var response;
function getSettings(){
    chrome.storage.sync.get(["grade","gradeStyle","finalColor"],function(res){
    response=res;
    });
    return res;
}
export {getSettings};
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!