Giter Site home page Giter Site logo

`.map` reply with a Future. about warp HOT 2 CLOSED

seanmonstar avatar seanmonstar commented on May 5, 2024
`.map` reply with a Future.

from warp.

Comments (2)

seanmonstar avatar seanmonstar commented on May 5, 2024

The and_then combinator is meant for returning a Future. The validation part is just that a user needs to explicitly handle their Future's error, and convert it into an appropriate rejection (or reply, if that's preferred).

from warp.

norcalli avatar norcalli commented on May 5, 2024

Ah, ok. I got it working, I believe. Essentially, as long as I map_err into warp::reject() or the like, it's accepted. The error message was a bit opaque, especially since CombineRejection is a sealed trait. For future reference, this is a working snippet from my code:

use std::time::Duration;

use futures::prelude::*;
use futures_timer::Delay;

use warp::Filter;

fn gpu_task(input: String) -> impl Future<Item = String> {
    Delay::new(Duration::from_millis(100)).map(move |()| input)
}

fn main() {
    let route = warp::path("batch")
        .and(warp::filters::body::json())
        .and_then(|input: String| {
            gpu_task(input)
                .map(|output| warp::reply::json(&output))
                .map_err(|_| warp::reject::server_error())
        });

    warp::serve(route).run(([0; 4], 3030));
}

from warp.

Related Issues (20)

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.