Skip to content

Commit 936337a

Browse files
committed
Async Lambda initializer
1 parent 6d2018c commit 936337a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Sources/AWSLambdaRuntimeCore/Lambda.swift

+19
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@ public enum Lambda {
5555
fatalError("\(error)")
5656
}
5757
}
58+
59+
#if compiler(>=5.4) && $AsyncAwait
60+
public static func run(_ factory: @escaping (InitializationContext) async throws -> Handler) {
61+
self.run { context -> EventLoopFuture<Handler> in
62+
@asyncHandler func _createLambda(_ context: InitializationContext, promise: EventLoopPromise<Handler>) {
63+
do {
64+
let handler = try await factory(context)
65+
promise.succeed(handler)
66+
} catch {
67+
promise.fail(error)
68+
}
69+
}
70+
71+
let promise = context.eventLoop.makePromise(of: Handler.self)
72+
_createLambda(context, promise: promise)
73+
return promise.futureResult
74+
}
75+
}
76+
#endif
5877

5978
/// Run a Lambda defined by implementing the `LambdaHandler` protocol provided via a factory, typically a constructor.
6079
///

0 commit comments

Comments
 (0)