Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
How do I load an Alpine Store with asynchronous values from a fetch call?

Trying to load an Alpine store with values (a list of arrays) from an API call. The fetch returns a promise, so looking for the best way to load this diretly:

document.addEventListener('alpine:init', () => {
    Alpine.store('myStore', {
        my_data: fetch('/api/1')
        .then(response => response.json())
    })
})

I’m not committed to fetch but whatever’s easiest to add to the store directly.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Invert it. Fetch the data first then create the store once it's resolved

document.addEventListener("alpine:init", async () => {
  const res = await fetch("/api/1");
  if (!res.ok) {
    throw new Error(`${res.status}: ${await res.text()}`);
  }
  Alpine.store("myStore", {
    my_data: await res.json(),
  });
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!