Skip to content

Instantly share code, notes, and snippets.

@drosenstark
Created February 21, 2022 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drosenstark/3e6f7f3c0cbcd544d15d1f16ae136998 to your computer and use it in GitHub Desktop.
Save drosenstark/3e6f7f3c0cbcd544d15d1f16ae136998 to your computer and use it in GitHub Desktop.
Extension for easy testing of async code
// Copyright 2022 Confusion Studios LLC
// by Dan Rosenstark
import Foundation
/// Extension on DispatchQueue? for Testing
/// If you don't have a DispatchQueue, run immediately (on current queue)
extension Optional where Wrapped : DispatchQueue {
public func asyncIfNotNil(execute block: @escaping ()->()) {
if let self = self {
self.async(execute: block)
} else {
// should only happen in DEBUG (for testing)
block()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment