question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@init and async function

See original GitHub issue

Look like if pass the async function to @init then storage doesn’t update. I have written small test for this:

it('update state with async @init', () => {
  function init (store) {
    store.on('@init', async () => ({ a: 1, c: 2 }))
  }
  let store = createStore([init])
  expect(store.get()).toEqual({ a: 1, c: 2 }) // here will be empty object
})

How I can handle it? I need it in https://github.com/storeon/localstorage because some storage should be fired with await in @init.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
aicommented, Jan 23, 2020

You need to wait that 500 ms before calling expect(). Try:

- it('update state with async @init', () => {
+ it('update state with async @init', async () => {
    function init (store) {
      store.on('@init', async () => {
        await delay(500)
        store.dispatch('update/data', { a: 1, c: 2 })
      })
      store.on('update/data', (_, data) => data)
    }
    let store = createStore([init])
+   await delay(600)
    expect(store.get()).toEqual({ a: 1, c: 2 }) // here will be empty object
  })
1reaction
aicommented, Jan 22, 2020

You need to dispatch another event and change data there:

store.on('@init', async () => {
  let data await loadData()
  store.dispatch('data/loaded', data)
})

store.on('data/loaded, data => {
  return { data }
})

We added this limit for a purpose to always have an event in history on any state changes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set class attribute with await in __init__ - Stack Overflow
class aobject(object): """Inheriting this class allows you to define an async __init__. So you can create objects by doing something like `await ......
Read more >
async function expression - JavaScript - MDN Web Docs
An async function expression can be used as an IIFE (Immediately Invoked Function Expression) which runs as soon as it is defined. See...
Read more >
Initialize app with an Async function | SwiftUI - Apple Developer
I need my app to configure the backend at start, here's the function to do so: // Initializes Amplify final func configureAmplify() async...
Read more >
The Proper Way to Write Async Constructors in JavaScript
The static async factory function pattern allows us to emulate asynchronous constructors in JavaScript. At the core of this pattern is the ...
Read more >
How to do asynchronous operation in object constructor
Approach 2 - Use async initializer() method ... Another approach is to dedicate the constructor for initializing variables and have a separate asynchronous...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found