Giter VIP home page Giter VIP logo

async-await's Introduction

Async-Await

Crates.io

Warning Deprecated in favour to the standard RFC that just has been approved:

https://github.com/rust-lang/rfcs/blob/master/text/2394-async_await.md

Just two macros to emulate a simple Async and Await using Futures (provided by futures-rs thanks to @cmgriffing for port it).

Usage

This is available in crates.io. Add this to your Cargo.toml:

[dependencies]
async-await = "0.2.1"

Example

Here is a simple example, you need to do the #[macro_use] and use async_await::*; because of the expansion of the macros :)

#[macro_use]
extern crate async_await;

use async_await::*;

fn main() {
    let computation = async!{"Hello world!"};
    println!("{}", await!(computation));
}

Another example using hyper, a shared client and a block in async :)

#[macro_use]
extern crate async_await;
extern crate hyper;

use std::io::Read;
use std::sync::Arc;

use async_await::*;

use hyper::Client;
use hyper::header::Connection;

fn main() {
    let client = Arc::new(Client::new());

    let client_comp = client.clone();
    let computation = async!{{
        let mut res = client_comp.get("http://rust-lang.org/")
            .header(Connection::close())
            .send().unwrap();
        let mut body = String::new();
        res.read_to_string(&mut body).unwrap();
        body
    }};

    println!("Before await!");
    println!("{}", await!{computation});
    println!("After await!");
}

You can also provide a default value in case that the computation fails:

#[macro_use]
extern crate async_await;

use async_await::*;

fn main() {
    let computation = async!{panic!(":()")};
    assert_eq!("Recovered!", await!{computation, "Recovered!"});
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

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.