Function Invocation (@microfn/fn)
@microfn/fn
Section titled “@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
Section titled “Import”import fn from "@microfn/fn";
Quick Snippet
Section titled “Quick Snippet”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 };}
Examples
Section titled “Examples”Here are a few useful things you can do with @microfn/fn
:
1. Chain functions in a workflow
Section titled “1. Chain functions in a workflow”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 };}
2. Execute functions in parallel
Section titled “2. Execute functions in parallel”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 };}
3. Implement a Map-Reduce pattern
Section titled “3. Implement a Map-Reduce pattern”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;}