Skip to content

Instantly share code, notes, and snippets.

@tornqvist
Last active March 7, 2018 10:44
Show Gist options
  • Save tornqvist/d7e65cfe97718f19d1aa01f0877795e7 to your computer and use it in GitHub Desktop.
Save tornqvist/d7e65cfe97718f19d1aa01f0877795e7 to your computer and use it in GitHub Desktop.
Choo async render issue
var fetch = require('node-fetch')
var html = require('choo/html')
var choo = require('choo')
var app = choo()
app.route('/', main)
app.use(store)
function main (state, emit) {
if (!state.data) {
emit('fetch:data')
return html`<body><h1>Loading</h1></body>`
}
return html`
<body>
<h1>Hello ${state.data}!</h1>
</body>
`
}
function store (state, emitter) {
emitter.on('fetch:data', function () {
var request = fetch('/some-api').then(function (response) {
return response.text().then(function (data) {
state.data = data // <-- reference to state is outdated as toString doesn't mutate app.state
emitter.emit('render')
})
})
if (state._experimental_prefetch) {
state._experimental_prefetch.push(request)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment