Skip to content

Commit 25d29f5

Browse files
committedMay 17, 2024
improvement: pass down file/line via makeFutureWithResultOfTask
In some circumstances it can be difficult to know which specific call to makeFutureWithTask leaked a promise. Adds a new function that passes down file/line to help aid debugging such situations.
1 parent 609469f commit 25d29f5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
 

‎Sources/NIOCore/AsyncAwaitSupport.swift

+22
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,32 @@ struct AsyncSequenceFromIterator<AsyncIterator: AsyncIteratorProtocol>: AsyncSeq
338338

339339
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
340340
extension EventLoop {
341+
@available(*, deprecated, renamed: "makeFutureWithResultOfTask", message: "Prefer makeFutureWithResultOfTask as it will track the original creator of the promise.")
341342
@inlinable
342343
public func makeFutureWithTask<Return>(_ body: @Sendable @escaping () async throws -> Return) -> EventLoopFuture<Return> {
343344
let promise = self.makePromise(of: Return.self)
344345
promise.completeWithTask(body)
345346
return promise.futureResult
346347
}
347348
}
349+
350+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
351+
extension EventLoop {
352+
/// Make a future whose body runs in an `async` function `body`.
353+
///
354+
/// This function can be used to bridge the `async` world into an `EventLoopFuture`.
355+
///
356+
/// - parameters:
357+
/// - body: The `async` function to run.
358+
/// - returns: A `Future` which completes when `body` finishes running.
359+
@inlinable
360+
public func makeFutureWithResultOfTask<Return>(
361+
file: StaticString = #fileID,
362+
line: UInt = #line,
363+
_ body: @Sendable @escaping () async throws -> Return
364+
) -> EventLoopFuture<Return> {
365+
let promise = self.makePromise(of: Return.self, file: file, line: line)
366+
promise.completeWithTask(body)
367+
return promise.futureResult
368+
}
369+
}

0 commit comments

Comments
 (0)