Code Monkey home page Code Monkey logo

Comments (10)

DavideCarvalho avatar DavideCarvalho commented on May 9, 2024

Tried taking off async and toPromise() from makeRequest, but still outputs the same error

export class AppComponent implements OnInit {
  title = 'app';

  private makeRequest () {
    return this.http.get(`https://jsonplaceholder.typicode.com/posts/1`);
  }

  constructor(private http: HttpClient) {}

  ngOnInit() {
    const route = 'https://jsonplaceholder.typicode.com/posts/1';
    const circuitBreakerOptions = {
      timeout: 500,
      maxFailures: 3,
      resetTimeout: 5000
    };
    const circuit = circuitBreaker(this.makeRequest, circuitBreakerOptions);
    circuit.fallback(() => `unavailable right now. Try later.`);
    circuit.on('success', (result) => JSON.stringify(result));
    circuit.fire().catch((e) => console.error(e));
  }
}

from opossum.

DavideCarvalho avatar DavideCarvalho commented on May 9, 2024

So i thought that since Angular http module returns an Observable and not a Promise, I added axios to the project. Still gives me the error, tho

export class AppComponent implements OnInit {
  title = 'app';

  constructor(private http: HttpClient) {}

  private makeRequest(): Promise<any> {
    return this.http.get(`https://jsonplaceholder.typicode.com/posts/1`).toPromise();
  }

  ngOnInit() {
    this.makeRequest()
    .then((data) => {
      console.log(data);
    });
    const circuitBreakerOptions = {};
    const circuit = circuitBreaker(() => axios.get(`https://jsonplaceholder.typicode.com/posts/1`), circuitBreakerOptions);
    circuit.fallback(() => `unavailable right now. Try later.`);
    circuit
      .fire()
      .then(data => console.log(data))
      .catch((e) => console.error(e));
  }
}

So I tried to put the code on another enviroment. Copied and pasted the code on a repl and it worked!
Since i'm at work right now, the network is blocking repl.it from saving the code, but i'll paste it here
the site is: https://repl.it

let opossum = require('opossum');
let axios = require('axios');
const makeRequest = () => {
	return axios.get(`https://jsonplaceholder.typicode.com/posts/1`);
};

// axios.get(`https://jsonplaceholder.typicode.com/posts/1`)
// .then(data => console.log(data))
// .catch(error => console.error(error))

const circuitBreakerOptions = {};
const circuit = circuitBreaker(
	() => axios.get(`https://jsonplaceholder.typicode.com/posts/1`),
	circuitBreakerOptions
);
circuit.fallback(() => `unavailable right now. Try later.`);
circuit.fire().then(data => console.log(data)).catch(e => console.error(e));

from opossum.

lance avatar lance commented on May 9, 2024

This looks like it could be a transpilation issue with TypeScript. Would it be possible for you to provide a small piece of code that would allow me to reproduce the error locally?

from opossum.

DavideCarvalho avatar DavideCarvalho commented on May 9, 2024

Here's the repo with the code I was trying to run:
https://github.com/DavideCarvalho/angular-opossum-issue

from opossum.

lance avatar lance commented on May 9, 2024

I've reproduced this, and it's definitely something going on between the typescript compilation and webpack-ing. But I can't quite figure out what it is.

I'm not very familiar with Angular, but it seems that using the window object is frowned upon. You can see here where opossum sets the circuitBreaker object in either the global window object or in module.exports. I can see in the browser console, that window.circuitBreaker exists and is a function. But I don't understand Angular and how it works well enough to know why it's not being assigned in the import statement.

from opossum.

lance avatar lance commented on May 9, 2024

@DavideCarvalho I have pushed a branch which I believe addresses your issues. Can you please test against https://github.com/bucharest-gold/opossum/tree/182-fix-browser-for-angular? If this works for you, I will merge and push a new release.

from opossum.

DavideCarvalho avatar DavideCarvalho commented on May 9, 2024

Okay, Iā€™l test it when I get home. Thank you for everything

from opossum.

DavideCarvalho avatar DavideCarvalho commented on May 9, 2024

Hello lance, sorry for the wait. I've managed to test the branch right now and worked like a charm!

from opossum.

lance avatar lance commented on May 9, 2024

@DavideCarvalho good news. I have a couple of other tasks to take care of, and then I will spin a new release.

from opossum.

lance avatar lance commented on May 9, 2024

@DavideCarvalho I have merged the pull request and published 1.5.0. Enjoy!

from opossum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.