Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
Get data stored in AsyncStorage instantly without async/await(promise)

config.js

AsyncStorage.getItem("storedValue").then(value=>{
//return data based on the value from asyncStorage storage
 if(value==="value1"){
  return {
   text:"text1",
   number:"number1"
  }
 }
 else if(value==="value2"){
  return {
   text:"text2",
   number:"number2"
  }
 }
})

index.js

import config from "./config"
//I have to use data from config file here
console.log(config.text) //->getting error
//The problem is that, since fetching from AsyncStorage is asynchronous, data from config is not yet available here


//I have tried SyncStorage npm package for react native. It loads all the asyncStored values into memory upon calling
await SyncStorage.init() //-> called on app initialisation

//And can be used later in place of AsyncStorage.getItem(""), which returns the value directly instead of promise
const value=SyncStorage.get("storedValue")

But the problem is, I am accessing the config in index.js(which is the starting file for a react-native project). And I have to initialise SyncStorage here, which is again asynchronous, which means the config file will get loaded before init() happens.

What I am expecting is:

  1. get the value stored in local storage instantly without async/await in index.js OR
  2. In the app, I will have a button to restart the app. But, should be able to pass a value during the restart, which can be used in config.js or index.js while the app loadsOR
  3. Or use any other package for deep-linking or something like that, so that on app restarting app using deep-link, the value passed should be accessible. It has to work for both android and iOS

Changes that are not possible:

  1. Cannot make any other file other than index.js as starting file because, many other project packages are being initialized here, and in fact, the stored value we are trying to access needs to be passed to these packages while initializing.

Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Please explain a little further...

Summarising what I understand from your question:

  • you want to read some value from async storage right away in index
  • you tried using synch storage for the same
  • you cannot move your code from index because you parked everything there

Trying to untangle that here my answer:

  • it is not really best practice to put a lot of code in app.js or index.js so I would try to avoid that as much as possible

  • sync storage as well as redux are normally just available at runtime and often are loaded from the async storage

  • async storage needs time and thus also needs to be loaded either with a then promise look or await catch

--> If I was you I would use async storage as normal with await and introduce some sort of splash screen where you do the loading.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda