Skip to content

Instantly share code, notes, and snippets.

View robmathers's full-sized avatar

Rob Mathers robmathers

View GitHub Profile
@drewmccormack
drewmccormack / AsyncSerialQueue.swift
Last active July 24, 2024 17:17
Proof of concept for a serializing queue for async funcs in Swift. It orders the calls, but also ensures each call is executed to completion before the next starts, thus avoiding interleaving races which can arise in multiple calls to an async func.
/// A queue that executes async functions in order, and atomically.
/// That is, each enqueued func executes fully before the next one is started.
struct AsyncSerialQueue {
typealias Block = () async throws -> [Int]
private struct Item {
let block: Block
let continuation: CheckedContinuation<[Int], Error>
}