Problem in chaining futures

Hello,

I am developing an application which does the following job:
* Read http request from client
* Parse request body from request
* Pass request body values via mpsc channel and returns immediate 'response' to the client. Another future will take care of reading messages from this mpsc channel and process them.

Sample code:

        (&Method::Post, "/postTest") => {
			Box::new(req.body().concat2().map(|b| {
				let bad_request: &[u8] = b"Missing field";
				let json: serde_json::Value =
					if let Ok(j) = serde_json::from_slice(b.as_ref()) {
						j
					} else {
						return Response::new().with_body(bad_request);
					};

				// use json value and send requests via mpsc channel
				let cmd = format!("Test: {}", json["cmd"]);
				tx_inst.clone().send(cmd.to_owned()).and_then(move |tx_inst| {
							return Response::new()
						.with_body("Success");
					})
					.or_else(|_| {
						return Response::new()
						.with_body("Failure");
					})
			}))
		}

'tx_inst' is of type: mpsc::Sender
However the above code produces the following error.

The "tx_inst.clone().send" code works fine in a different context. It seems to have problem when it is chained like above. Tried replacing "and_then, or_else" part with "map, map_err". But this throws the following error.

Could anyone help me with this?

I’m on mobile so a bit brief.

You’ll want to change that map to and_then since the closure body is actually yielding another future. In the body, you’re right that you want to map the result of the send() call to a hyper Response. So you’ll end up with a chained future that ultimately resolves to the response.

1 Like