Skip to content

enhance action does not support async function as its parameter #7607

@teemingc

Description

@teemingc

Describe the bug

first pointed out in #7581 by @NormandoHall

Passing an async function as the parameter for the enhance action throws an error. Therefore, the async function runs but fails to return the callback function that is called after the form submission.

<form use:enhance={async () => {
  // function runs as as expected
  
  // ...
  
  // the returned callback function never runs
  return () => {} 
}}>

The browser error below displays because the returned value of an async function becomes a Promise instead of the expected callback function.
CleanShot 2022-11-11 at 20 43 24@2x

I've submitted a PR to await the function passed to the enhance action parameter.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-xk6efm?file=src%2Froutes%2F%2Bpage.svelte&terminal=dev

Submitting the form causes an error in the browser console and the returned callback never runs.

Logs

forms.js?v=48b077e5:100

Uncaught (in promise) TypeError: callback is not a function
    at HTMLFormElement.handle_submit (forms.js?v=48b077e5:100:3)

handle_submit	@	forms.js?v=48b077e5:100

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.87 
    @sveltejs/kit: next => 1.0.0-next.539 
    svelte: ^3.46.0 => 3.53.1 
    vite: ^3.1.0 => 3.2.3

Severity

serious, but I can work around it

Additional Information

The current workaround is to implement the progressive enhancement manually.

https://kit.svelte.dev/docs/form-actions#progressive-enhancement-custom-event-listener

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions