Skip to content

Function Invocation (@microfn/fn)

The @microfn/fn module allows you to call other functions in your workspace. This lets you build composable workflows by chaining multiple functions together, creating powerful and modular applications.

import fn from "@microfn/fn";
async function callAnotherFunction({ functionName, input }) {
// Execute another function by its name and pass input to it
const result = await fn.executeFunction(functionName, input);
return { result };
}

Here are a few useful things you can do with @microfn/fn:

Create a sequence of operations for a multi-step process like handling an order.

import fn from "@microfn/fn";
export default async function main({ orderId, amount }) {
// Step 1: Process payment
const paymentResult = await fn.executeFunction("my-user/process-payment", { orderId, amount });
if (!paymentResult.success) {
throw new Error("Payment failed");
}
// Step 2: Update inventory
await fn.executeFunction("my-user/update-inventory", { orderId });
// Step 3: Send confirmation email
await fn.executeFunction("my-user/send-confirmation-email", { orderId });
return { success: true, orderId };
}

Run multiple independent operations at the same time to speed up data fetching.

import fn from "@microfn/fn";
export default async function main({ userId }) {
// Fetch user profile, preferences, and activity in parallel
const [profile, preferences, activity] = await Promise.all([
fn.executeFunction("my-user/get-user-profile", { userId }),
fn.executeFunction("my-user/get-user-preferences", { userId }),
fn.executeFunction("my-user/get-user-activity", { userId })
]);
return { profile, preferences, activity };
}

Process a large number of items in parallel (Map) and then aggregate the results (Reduce).

import fn from "@microfn/fn";
export default async function main({ items }) {
// Map: Call a processing function for each item in parallel
const processingPromises = items.map(item =>
fn.executeFunction("my-user/process-item", { item })
);
const processedResults = await Promise.all(processingPromises);
// Reduce: Call an aggregation function with all the results
const finalReport = await fn.executeFunction("my-user/aggregate-results", {
results: processedResults
});
return finalReport;
}