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

184
Vistas
Event Source Fetch not returning Data

Below is the code of the Service

import { fetchEventSource } from '@microsoft/fetch-event-source';

export const AlertFetchEventSource = () => {
  fetchEventSource('https://puppygifs.tumblr.com/api/read/json'),
    {
      onmessage(ev) {
        const data = JSON.parse(ev.data);
        return data;
      },
    };
};

export default { AlertFetchEventSource };

index.tsx where I am making the call to this service but its not returning any data

import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';
import backendService from './services/backendService';

interface AppProps {}
interface AppState {
  name: string;
}

class App extends Component<AppProps, AppState> {
  constructor(props) {
    super(props);
    this.state = {
      name: 'React',
    };
    console.log(backendService.AlertFetchEventSource());
  }

  render() {
    return (
      <div>
        <Hello name={this.state.name} />
        <p>Start editing to see some magic happen :)</p>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Given the library you're using is an event handler, it doesn't ever actually return data. You'd need to provide it a callback to execute when it receives events.

// you might want to specify a better type than `any`
type MessageHandler = (data: any) => void;

export const AlertFetchEventSource = (onEvent: MessageHandler) => {
  fetchEventSource('https://puppygifs.tumblr.com/api/read/json'), {
    onmessage(ev) {
      const data = JSON.parse(ev.data);
      onEvent(data);
    },
  }); // you had a typo here, missing ")"
};

and use it like

import { AlertFetchEventSource } from "./services/backendService";

// snip

AlertFetchEventSource(data => {
  // handle event data, eg
  console.log(data);

  // or perhaps something like
  this.setState(data);
});
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