Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass argument to readable store in Svelte

is there someway to pass an argument to readable store in svelte ? I have the next code:

export const worker = readable(
    [], async (set) => {

        const response = await fetchAPI()
        set(response)
        
        const interval = setInterval(async () => {
            const response = await fetchAPI()
            set(response)
        }, 10000)
})

I want pass an argumeto to that readable function with the response of the api to set the result of it via set().

Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can access any variables that are in scope, so you can "pass" a variable from outside the readable‘s function.

export function worker(arg) {
   return readable([], async set => {
      const response = await fetchAPI(arg); // arg is in scope here
      // ...
   });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading