cylc / cylc-ui

Web app for monitoring and controlling Cylc workflows
https://cylc.github.io
GNU General Public License v3.0
37 stars 27 forks source link

[discussion] Should we adopt ES2017 or beyond in the code base? #136

Closed kinow closed 4 years ago

kinow commented 5 years ago

Hi,

We are using ES2015 (I think, though I may have slipped a Promise.finally somewhere, which is ES2018 I guess?) in the project.

With ES2015, we have Promises, and our code should look something like:

  getSuites () {
    return this.apolloClient.query({
      query: suitesQuery,
      fetchPolicy: 'no-cache'
    }).then((response) => {
      const suites = response.data.workflows
      return store.dispatch('suites/setSuites', suites)
    }).catch((error) => {
      const alert = new Alert(error.message, null, 'error')
      return store.dispatch('setAlert', alert)
    })
  }

Every time we have an asynchronous Promise, we can call it and use .then() to resolve the code once the promise has been fulfilled.

ES2017 added async and await for syntactical sugar in the language. The same code would then look like:

  async getSuites () {
    try {
      const graphqlResponse = await this.apolloClient.query({
        query: suitesQuery,
        fetchPolicy: 'no-cache'
      })
      await store.dispatch('suites/setSuites', graphqlResponse.data.workflows)
    } catch (error) {
      await store.dispatch('setAlert', new Alert(error.message, null, 'error'))
    }
  }

I am asking because @MartinRyan used this new syntax in the Graph.vue component, and I had added a post-it to check whether we should use Promises there, or if others would prefer to up the version we are supporting in Cylc UI.

Here's the browser compatibility for async functions:

image

Though Babel is able to transpile it to ES5/6/etc. But if we decide that async/await is the way to go, we can slowly start to update the code base... I'm a bit slow to pick up features... wrote Java5 code for a long time, and now I am still writing Java 8 code, even though 9/10/11 have new features :grimacing:

Thoughts?

oliver-sanders commented 5 years ago

For the codebase:

For the distribution:

I think currently babel is downgrading our code to some ancient standard, I think we can safely up the output to ES6.

kinow commented 4 years ago

So long as babel can handle it we can do whatever.

+1 after using the duo webpack+babel for a while, it looks pretty stable, and had so far no issues producing ES6 compatible output code.

Closing :tada: