Skip to main content

Functions

Wing supports two function types preflight and inflight.

  • Preflight: Code that runs once, at compile time, and generates the infrastructure configuration of your cloud application. For example, setting up databases, queues, storage buckets, API endpoints, etc.

  • Inflight: Code that runs at runtime and implements your application's behavior. For example, handling API requests, processing queue messages, etc. Inflight code can be executed on various compute platforms in the cloud, such as function services (such as AWS Lambda or Azure Functions), containers (such as ECS or Kubernetes), VMs or even physical servers.

By default functions are preflight functions.

main.w
bring cloud;

// preflight function - can be called at compile time
let plus = (num1: num, num2: num) => {
return num1 + num2;
};

// Inflight code here is run at runtime
let func = new cloud.Function(inflight (payload:Json?) => {
// When when the function it excuted on the cloud
log(Json.stringify(payload));
});

let value = plus(1, 2);

log(value); // 3

Inflight modifiers

main.w
let handler = inflight (message: str): void => {
// using the inflight modifier
let dup = inflight (s: str, count: num) => {
// code
};
// inflight modifier is not required when function is declared in inflight context
let sup = (s: str, count: num) => {
// code
};
};