Define Steps step.run()
Use step.run()
to run synchronous or asynchronous code as a retryable step in your function. step.run()
returns a Promise that resolves with the return value of your handler function.
export default inngest.createFunction(
{ id: "import-product-images" },
{ event: "shop/product.imported" },
async ({ event, step }) => {
const uploadedImageURLs = await step.run("copy-images-to-s3", async () => {
return copyAllImagesToS3(event.data.imageURLs);
});
}
);
step.run(id, handler): Promise
- Name
id
- Type
- string
- Required
- required
- Description
The ID of the step. This will be what appears in your function's logs and is used to memoize step state across function versions.
- Name
handler
- Type
- function
- Required
- required
- Description
The function that code that you want to run and automatically retry for this step. Functions can be:
- A synchronous function
- An
async
function - Any function that returns a
Promise
Throwing errors within the handler function will trigger the step to be retried (reference).
// Steps can have async handlers
const result = await step.run("get-api-data", async () => {
// Steps should return data used in other steps
return fetch("...").json();
});
// Steps can have synchronous handlers
const data = await step.run("transform", () => {
return transformData(result);
});
// Returning data is optional
await step.run("insert-data", async () => {
db.insert(data);
});
How to call step.run()
As step.run()
returns a Promise, you will need to handle it like any other Promise in JavaScript. Here are some ways you can use step.run()
in your code:
// Use the "await" keyword to wait for the promise to fulfil
await step.run("create-user", () => {/* ... */});
const user = await step.run("create-user", () => {/* ... */});
// Use `then` (or similar)
step.run("create-user", () => {/* ... */})
.then((user) => {
// do something else
});
// Use with a Promise helper function to run in parallel
Promise.all([
step.run("create-subscription", () => {/* ... */}),
step.run("add-to-crm", () => {/* ... */}),
step.run("send-welcome-email", () => {/* ... */}),
]);
Return values & serialization
All data returned from step.run
is serialized as JSON. This is done to enable the SDK to return a valid serialized response to the Inngest service. You will be able to customize this and utilize formats like superjson
with the addition of middleware in the Inngest SDK's 2.0 release.
const output = await step.run("create-user", () => {
return { id: new ObjectId(), createdAt: new Date() };
});
/*
{
"id": "647731d1759aa55be43b975d",
"createdAt": "2023-05-31T11:39:18.097Z"
}
*/
Usage limits
See usage limits for more details.