d4f / backbone-highway

Routing Backbone with style \o/
MIT License
19 stars 5 forks source link

Refactor async route execution using async/await syntax #45

Open ghost opened 6 years ago

ghost commented 6 years ago

Refactor the whole asynchronous route action control flow using async/await syntax and make the go method return a Promise.

⚠️This will break how the current action control flow works with the state.resolve() and state.reject() methods.

This refactor should be released as the v3.

import highway from 'backbone-highway'

highway.route({
  name: 'profile',
  path: '/users/:name',
  action: async (state) => {
    console.log(`Hello ${state.params.name}`)

    // Get some user data
    const data = await fetch(`/api/users/${state.params.name}`)

    return data.id // 42
  }
})

const userId = await highway.go({ name: 'profile', params: { name: 'rascarlito' })

console.log('User id #', userId) // User id #42

I don't think actually getting data from calling the router should actually be very useful, but listening to when the route action has actually finished executing is.

ghost commented 6 years ago

Maybe re-implement or pull in things from https://github.com/d4f/backbone-highway/issues/31#issuecomment-298473083