How to wait for the response of an action
This guide shows you how to run an action from your application and show its response. You need an action in your project first, with a key and a workflow that the action starts. See Actions.
The response is the value that the action returns. For an action that starts a workflow in Picked, the response is the payload of the Action response node in the workflow.
1. Generate the types of your project
Run type generation. The generated types name the actions that start a workflow. For those
keys, the types of useAction and getAction include run. At runtime, the value includes
run for each key. See Type generation.
2. Bind the action to a component
Call useAction in React, or getAction in Svelte. Neither runs the action. The action runs
only when you call run. The action receives the arguments of run in args.
All the components that use the same key and the same experience share one run state. When
one of them calls run, all of them show that run.
import { useAction } from '@picked/react/hooks'
function CallbackButton() {
const callback = useAction('request-callback')
return (
<button onClick={() => void callback.run()} disabled={callback.status === 'running'}>
Get a call back
</button>
)
}<script lang="ts">
import { getAction } from '@picked/svelte/hooks'
const callback = getAction('request-callback')
</script>
<button onclick={() => $callback.run()} disabled={$callback.status === 'running'}>
Get a call back
</button>3. Show the response
Read status, response and error for the last run. Compare response with the values
that your workflow sends.
import { useAction } from '@picked/react/hooks'
// Reads the status that the workflow sends in its response.
function answerStatus(response: unknown): unknown {
return typeof response === 'object' && response !== null && 'status' in response
? response.status
: undefined
}
function CallbackButton() {
const callback = useAction('request-callback')
if (callback.status === 'failed') return <p>We could not send your request.</p>
if (callback.status === 'succeeded') {
if (answerStatus(callback.response) === 'closed') {
return <p>We call back on weekdays, 8 am to 5 pm.</p>
}
return <p>Thank you. We will call you soon.</p>
}
return (
<button onClick={() => void callback.run()} disabled={callback.status === 'running'}>
Get a call back
</button>
)
}<script lang="ts">
import { getAction } from '@picked/svelte/hooks'
const callback = getAction('request-callback')
// Reads the status that the workflow sends in its response.
function answerStatus(response: unknown): unknown {
return typeof response === 'object' && response !== null && 'status' in response
? response.status
: undefined
}
</script>
{#if $callback.status === 'failed'}
<p>We could not send your request.</p>
{:else if $callback.status === 'succeeded' && answerStatus($callback.response) === 'closed'}
<p>We call back on weekdays, 8 am to 5 pm.</p>
{:else if $callback.status === 'succeeded'}
<p>Thank you. We will call you soon.</p>
{:else}
<button onclick={() => $callback.run()} disabled={$callback.status === 'running'}>
Get a call back
</button>
{/if}run does not reject. It resolves to { status: 'succeeded', response } or to
{ status: 'failed', error }.
4. Run the action outside a component
Call client.actions.run with the action key. It resolves to the response, and it rejects
when the action fails.
The client registers the actions of your project when it loads the configuration of your
project. For this, the client must have dynamicExperiencesPlugin, and you must start the
client. Until the client is ready, run rejects with Action '<key>' is not registered. A
handler that your application registers in createClient({ actions }) is available
immediately.
import { createClient } from 'picked'
import { dynamicExperiencesPlugin } from '@picked/client/plugins/dynamic-experiences'
const picked = createClient({
projectId: 'your-project-id',
plugins: [dynamicExperiencesPlugin()],
})
picked.start()
// Waits until the client is ready.
async function whenReady(): Promise<void> {
if (picked.readyStore.getState()) return
await new Promise<void>((resolve) => {
const subscription = picked.readyStore.updates.watch((ready) => {
if (!ready) return
subscription.unsubscribe()
resolve()
})
})
}
async function requestCallback(): Promise<void> {
await whenReady()
try {
const response = await picked.actions.run('request-callback', { timeoutMs: 15_000 })
console.info('Callback response', response)
} catch (error) {
console.warn('Callback request failed', error)
}
}Set timeoutMs to stop the wait sooner. Set signal to stop the wait from your own code.
Related
- SDK actions — every outcome, error and limit.
- How to register an action handler — return a response from your own handler.
- How to call an external API from a workflow — a workflow that sends a response.
