dunnock / react-sigma

Lightweight React library for drawing network graphs built on top of SigmaJS
https://dunnock.github.io/react-sigma/
MIT License
260 stars 43 forks source link

Use async/await instead of callbacks #27

Open dunnock opened 7 years ago

dunnock commented 7 years ago
    _load(url: string) {
        sigma.parsers.gexf(
            this.props.path ,
            this.props.sigma ,
            this.onLoad
        )
    }

    _onLoad() {
        if(this.props.sigma)
        this.props.sigma.refresh()
        this.setState({loaded:true})
        if(this.props.onGraphLoaded)
            return this.props.onGraphLoaded()
    }

can be written as:

  loadGexf() {
    return new Promise((resolve) => sigma.parsers.gexf(this.props.path, this.props.sigma, resolve));
  }

  async load() {
    const sigma = this.props.sigma;
    if(!sigma) return;
    await loadGexf();
    sigmaI.refresh();
    this.setState({loaded:true});
    this.props.onGraphLoaded &&
      this.props.onGraphLoaded();
  }
avoloschuk commented 7 years ago

Is it just "copy&paste" replacement throughout the code?

dunnock commented 7 years ago

No, basically you replace callbacks

sigma.parsers.gexf(
            this.props.path ,
            this.props.sigma ,
            this.onLoad
        )

with async/await:

  await loadGexf();

but it requires: