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

183
Views
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 answers
Answer question

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 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!