Git Product home page Git Product logo

nioasync's Introduction

NIOAsync

โš ๏ธ setjmp and longjpm used to implement SwiftCoroutine are generally considered to be undefined behaviour. It's probably a bad idea to use this project for anything other than experimentation. Use at your own risk.

๐Ÿ“ฆ Getting Started

Oh, you're still reading, huh? Alright, I guess I'll get you up to speed. This project is pretty small, so there isn't much to it. Start by adding it as a dependency to your SPM project:

.package(url: "https://github.com/calebkleveter/NIOAsync.git", from: "0.1.0")

Then run swift package update, or feel smug that you have Xcode 11 and wait for it to install.

๐Ÿ”จ API

There are really 2 methods that this package provides. The first is EventLoop.async(_:):

let result: EventLoopFuture<OperationResult> = eventLoop.async {
    return someReallyLongAsyncOperation()
}

This method starts a coroutine and runs the provided closure inside of that coroutine. The value that is returned from the closure is the result that you get in the EventLoopFuture that is returned. The closure also allows you to throw errors. If that happens, your EventLoopFuture will be failed.

The second piece to the puzzel is EventLoopFuture.await():

let user = try User.query(on: database).first().await()

Note that this method is different from the wait method provided by NIO. You can only call it inside the closure of an async call (or inside any other coroutine that you happen to have started). It waits for the future result and returns it, without blocking the current thread because you are in a coroutine.

In the end, your code might look a little like this:

eventLoop.async {
    guard let user = try User.query(on: database).filter(\.$id == id).first().await() else {
        throw Abort(.notFound)
    }
    guard let id = user.id else {
        throw Abort(.internalServerError, reason: "All saved models must have an ID")
    }
    
    return try s3Client.fetch("\(id)/profile-image.png").await()
}

๐Ÿ“„ License

All code, documents, and other resources contained in this repository is under the MIT license agreement.

nioasync's People

Contributors

calebkleveter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nioasync's Issues

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.