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

185
Vistas
React: async/await function returns undefined then value I want

I have this async function:

Which returns undefined on the first render and then the data from the fetch:

const [
  MessageListDataProvider,
  useMessageListData
] = constate(() => {
  const [messagePath, useMessagePath] = useState(api.UNRESOLVED_MESSAGES_PATH)
  const getMessagePath = (newPath) => {
    useMessagePath(newPath)
  }
  const [messages, setMessages] = useState({})

  const {
    value,
    loading,
    error,
    retry: refresh
  } = useAsyncRetry(async () => {
    if (!messages[messagePath] && !messages.links ) {
      return await api.fetchAllMessages(messagePath).then(async (data) => {
        const localData = await data
        let localMessages = { ...messages }
        localMessages = { ...localMessages, [messagePath] : localData.messages }
        localMessages = { ...localMessages, ['links'] : localData.links }
        console.log('localMessages', localMessages)
        return await localMessages
      })
    }
  }, [messagePath])
  
  console.log('value', value)

  return {
    getMessagePath,
    value,
    loading,
    error,
    refresh
  }
})

value undefined

MessageListView.jsx:23 value 23 undefined

MessageListDataProvider.js:34 value undefined

MessageListView.jsx:23 value 23 undefined

MessageListDataProvider.js:28 localMessages {/messages?status=Unresolved&limit=100&offset=0: Array(64), links: {…}}

MessageListDataProvider.js:34 value {/messages?status=Unresolved&limit=100&offset=0: Array(64), links: {…}}

MessageListView.jsx:23 value 23 {/messages?status=Unresolved&limit=100&offset=0: Array(64), links: {…}}

How can I prevent the return value from being undefined as it's causing me not to get any day in my <MessageListView /> component.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need a loading state. Initiate state to loading, and then inside MessageListView, display a loader when the value is undefined. Or conditionally do not display MessageListView at all, but a loader instead when state is set to loading. Also you might want to wrap the async call into a react useEffect callback, as it's a side-effect.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The async function will return undefined when its "if" statement fails - you should probably be handling that case better:

async () => {
    if (!messages[messagePath] && !messages.links ) {
        ...
    } else {
        return { /* something that's not undefined! */ }
    }
}
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