Giter VIP home page Giter VIP logo

combineasyncually's Introduction

CombineAsyncually

This is a DEMONSTRATION of how you can bridge the new async / await functionality in Swift 5.5 with Combine.

There is NO WARRANTY. There is also a high chance this code may set your Mac, iPhone or other device on fire.

It provides three simple utilities that let you go mix and match your async functions and Combine publishers:

Async Function to Combine

These samples use the withPublisher1 and withThrowingPublisher functions to run an async closure and emit the result.

func myAsyncFunction() async -> String {
    return "Hello ๐Ÿ‘‹"
}

// For non-throwing functions
withPublisher {
    await myAsyncFunction()
}
    .sink {
        print($0)   // Hello ๐Ÿ‘‹
    }

// For throwing ones
withThrowingPublisher {
    try await someThrowingFunction()
}
    .catch {
        // handle error
    }
    .sink {
        // handle value
    }

Mixing Async Functions in Combine Publisher chains

You can use the .await and .tryAwait operators to mix and match your Combine publishers and async code.

// Non throwing functions
somePublisher
    .await { value in 
        return await someAsyncFunction(value)
    }
    .sink {
        // handle result of someAsyncFunction
    }
    
// Throwing ones
somePublisher
    .tryAwait { value in
        return try await someThrowingAsyncFunction(value)
    }
    .catch {
        // handle error
    }
    .sink {
        // handle result of someAsyncFunction
    }

Awaiting Your Publishers

You can also call .get() on supported Publishers2 to await for the Publisher to complete.

let result1 = try await Just("Hello!").get()            // Hello!    
let result2 = try await somePublisher.first().get()

Learning more

This repository is here to support a presentation given at Melbourne CocoaHeads on the 10th of June 2021. You can watch it here.

Notes

  1. They're named to align with the withTaskGroup and withThrowingTaskGroup (and friends) functions.
  2. We only support publishers that are guaranteed to emit once because this is just too dangerous. Use .first() to get around this.

combineasyncually's People

Contributors

bok- avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.