Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda