async-await

Why does fetch return a weird hash of integers - part 2?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-25 04:30:25
问题 I'm using async/await with React Native. My result from response.json() is: { _45: 0, _81: 0, _65: null, _54: null } For whatever reason, the actual response I want is located in _65 and I have no idea what these random keys are. It seems that it is related to the fact that .json() returns a Promise. componentDidMount() { this.getData().then(data => this.setState({ data })) } async getData() { try { let response = await fetch(myUrl) let json = await response.json() return json } catch(err) {

Make function not to wait for other function inside it

坚强是说给别人听的谎言 提交于 2020-01-25 04:19:08
问题 I have a flask service as below: from flask import Flask, request import json import time app = Flask(__name__) @app.route("/first", methods=["POST"]) def main(): print("Request received") func1() return json.dumps({"status": True}) def func1(): time.sleep(100) print("Print function executed") if __name__ == "__main__": app.run("0.0.0.0", 8080) So now when I make a request using http://localhost:8080/first control goes to main method and it prints Request received and wait for func1 to get

Parallel.ForEach or Task.WhenAll when involving async operations? [closed]

99封情书 提交于 2020-01-25 02:51:11
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 months ago . I've read the following closely related thread, but I'd like to ask about a more specific thing. If we need to run Tasks/methods asynchronously, and those tasks themselves run other tasks/await other tasks, which variant is prefered - Parallel.ForEach, or Task.WhenAll? I

Parse Nested JSON Using Axios and Async/Await

安稳与你 提交于 2020-01-25 01:01:07
问题 I am trying to fetch data from an api which should return a json object with an array of recipes. I am using React hooks on the front end and Axios to perform the request. The hook runs but I get an error while trying to access the json array from the response. Here's the sample api response: { count: 30, recipes: [objects, ... 29] } and my code to get the recipe response: const fetchRecipes = async () => { try { const recipeData = await Axios.get(api); console.log(recipeData); const recipes

System.Net.Http.HttpClient.PostAsync blocks and never returns

陌路散爱 提交于 2020-01-24 17:04:11
问题 I have a .NET framework Windows Forms application with a form that has this code: using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace test { public partial class Main : Form { public int exitCode = 1; private Options opts; CancellationTokenSource cancellationSource = new CancellationTokenSource(); public Main(Options opts) { InitializeComponent(); this.opts = opts; }

How to implement an efficient WhenEach that streams an IAsyncEnumerable of task results?

百般思念 提交于 2020-01-24 09:35:06
问题 I am trying to update my toolset with the new tools offered by C# 8, and one method that seems particularly useful is a version of Task.WhenAll that returns an IAsyncEnumerable. This method should stream the task results as soon as they become available, so naming it WhenAll doesn't make much sense. WhenEach sounds more appropriate. The signature of the method is: public static IAsyncEnumerable<TResult> WhenEach<TResult>(Task<TResult>[] tasks); This method could be used like this: var tasks =

When async-await change thread?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-24 01:22:15
问题 When I call test1() method then ManagedThreadId will be changed after Delay(1). When I call test2() method (instead of test1()) then ManagedThreadId will stay same. When async-await change thread? In test2() method time required to finish method is even longer then in test1() [Route("api/[controller]")] [HttpGet] public async Task<IActionResult> Get() { await MainAsync(); return new ObjectResult(null); } static async Task MainAsync() { Console.WriteLine("Main Async: " + System.Threading

TaskCanceledException when not awaiting

允我心安 提交于 2020-01-23 13:03:31
问题 I seem to be getting a TaskCanceledException whenever I return another Task synchronously instead of awaiting it, following the guidelines in When at last you await. TaskCanceledException code public static class Download { public static Task<byte[]> FromYouTubeAsync(string videoUri) { using (var client = new HttpClient()) { return FromYouTubeAsync( () => client .GetStringAsync(videoUri), uri => client .GetByteArrayAsync(uri)); } } public async static Task<byte[]> FromYouTubeAsync( Func<Task

Wrapping blocking calls to be async for better thread reuse and responsive UI

孤街醉人 提交于 2020-01-23 12:16:09
问题 I have a class that is responsible for retrieving a product availability by making call to a legacy class. This legacy class itself internally collects product data by making BLOCKING network calls. Note that I cannot modify code of legacy API. Since all products are independent to each other, I would like to parallelise collecting the information without creating any unnecessary threads and also not blocking thread that gets blocked on calling this legacy API. With this background here are

To Task.Run or not to Task.Run

∥☆過路亽.° 提交于 2020-01-23 06:49:48
问题 Suppose I have an interface which includes an async method, and I have two different implementations of that interface. One of the two implementations is naturally async, and the other is not. What would be the "most correct" way of implementing the non-async method? public interface ISomething { Task<Foo> DoSomethingAsync(); } // Normal async implementation public class Implementation1 : ISomething { async Task<Foo> ISomething.DoSomethingAsync() { return await DoSomethingElseAsync(); } } //