{#await data.myPromise}{:then results}{#each results as result}
  • {result}
  • {/each}{:catch error}

    {error?.message ?? \\\"Something went wrong\\\"}

    {/await}

    This is basically how the #await block works in svelte. It displays different content based on the state of a promise: a loading indicator while pending, results when resolved, and an error message if rejected.

    But let's say I want a certain function to run when the promise has been resolved or rejected (like a toast).


    Run (trigger) a function when the #await block resolves or rejects

    Here's how you can run specific functions when the promise resolves or rejects:

    {#await data.myPromise}{:then results}{showSuccess(results.length)}  {#each results as result}
  • {result}
  • {/each}{:catch error}{showError(error.message)}

    {error?.message ?? \\\"Something went wrong\\\"}

    {/await}

    Now, our function will run whenever the code block is reached.


    One more thing though...

    Fix undefined or any returned text showing up in browser

    When these functions run, whatever is returned text will show up in the browser, because it's kind of a workaround. The syntax we used is usually to meant to show returned strings/numbers in the browser. Even returning nothing will return the default undefined. And this string (which usually make no sense), will be displayed to the end user. Something like this:
    \\\"Running

    Makes no sense to the end user ?‍♂️?‍♀️

    So, make sure to return empty strings, or wrap the function in an hidden block:

    1. Method 1 (Return empty strings):

    In this method, we'll make sure to return empty strings from our functions.

    {#await data.myPromise}  {:then results}{showSuccess(results.length)} {#each results as result}
  • {result}
  • {/each}{:catch error}{showError(error.message)}

    {error?.message ?? \\\"Something went wrong\\\"}

    {/await}

    This will make sure empty strings are returned.

    --- Or ---

    2. Method 2 (Hide the returned text from the function in the UI with CSS.)

    In this method, we'll hide the function block in the UI instead, so the text returned is hidden from the user's sight.

    {#await data.myPromise}  {:then results}
    {showSuccess(results.length)}
    {#each results as result}
  • {result}
  • {/each}{:catch error}
    {showError(error.message)}

    {error?.message ?? \\\"Something went wrong\\\"}

    {/await}

    This CSS-based method will make sure that the returned text is hidden from sight.

    HappyHacking

    PS: Need to employ a SvelteKit Dev? Contact me

    ","image":"http://www.luping.net/uploads/20241118/1731929413673b254564c1a.png","datePublished":"2024-11-18T20:21:13+08:00","dateModified":"2024-11-18T20:21:13+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
    "If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"

    Running a Function When an #await Block resolves in Svelte(Kit)

    Published on 2024-11-18
    Browse:294

    Skip To Content:

    • About the #await block in svelte
    • Run (trigger) a function when the #await block resolves or rejects
    • Fix undefined or any returned text showing up in browser
      • 1. Method 1 (Return empty strings):
      • 2. Method 2 (Hide the returned text from the function in the UI with CSS.)
        • PS: Need to employ a SvelteKit Dev? Contact me

    About the #await block in svelte

    The #await block in svelte is very handy for handling asynchronous data:

    
    
    {#await data.myPromise}
    
    
    
    {:then results}
    
    {#each results as result}
    
  • {result}
  • {/each} {:catch error}

    {error?.message ?? "Something went wrong"}

    {/await}

    This is basically how the #await block works in svelte. It displays different content based on the state of a promise: a loading indicator while pending, results when resolved, and an error message if rejected.

    But let's say I want a certain function to run when the promise has been resolved or rejected (like a toast).


    Run (trigger) a function when the #await block resolves or rejects

    Here's how you can run specific functions when the promise resolves or rejects:

    
    
    {#await data.myPromise}
    
    
    
    {:then results}
    
    {showSuccess(results.length)}
    
      
    {#each results as result}
    
  • {result}
  • {/each} {:catch error} {showError(error.message)}

    {error?.message ?? "Something went wrong"}

    {/await}

    Now, our function will run whenever the code block is reached.

    • showSuccess is called when the promise resolves, with the number of results as an argument.
    • showError is triggered if the promise rejects, displaying a custom error message.

    One more thing though...

    Fix undefined or any returned text showing up in browser

    When these functions run, whatever is returned text will show up in the browser, because it's kind of a workaround. The syntax we used is usually to meant to show returned strings/numbers in the browser. Even returning nothing will return the default undefined. And this string (which usually make no sense), will be displayed to the end user. Something like this:
    Running a Function When an #await Block resolves in Svelte(Kit)

    Makes no sense to the end user ?‍♂️?‍♀️

    So, make sure to return empty strings, or wrap the function in an hidden block:

    1. Method 1 (Return empty strings):

    In this method, we'll make sure to return empty strings from our functions.

    
    
    {#await data.myPromise}
      
    
    
    {:then results}
    
    {showSuccess(results.length)} 
    
    
    {#each results as result}
    
  • {result}
  • {/each} {:catch error} {showError(error.message)}

    {error?.message ?? "Something went wrong"}

    {/await}

    This will make sure empty strings are returned.

    --- Or ---

    2. Method 2 (Hide the returned text from the function in the UI with CSS.)

    In this method, we'll hide the function block in the UI instead, so the text returned is hidden from the user's sight.

    
    
    {#await data.myPromise}
      
    
    
    {:then results}
    
    
    
    {#each results as result}
    
  • {result}
  • {/each} {:catch error}
    Release Statement This article is reproduced at: https://dev.to/digitaldrreamer/running-a-function-when-an-await-block-resolves-in-sveltekit-11ec?1 If there is any infringement, please contact [email protected] to delete it
    Latest tutorial More>

    Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

    Copyright© 2022 湘ICP备2022001581号-3