Lodash: is it possible to use map with async functions?

To process your response jsons in parallel you may use Promise.all: const responseJson = await response.json(); responseJson = _.sortBy(responseJson, “number”); let result = await Promise.all(_.map(responseJson, async (json) => await addEnabledProperty(json)) ); Since addEnabledProperty method is async, the following also should work (per @CRice): let result = await Promise.all(_.map(responseJson, addEnabledProperty));

Asynchronous method that does nothing

Just use Task.CompletedTask to return a completed task: public Task BeginAsync() { return Task.CompletedTask; } If you have a Task<TResult> use Task.FromResult<TResult> to return a completed task with a result: public Task<bool> BeginAsync() { return Task.FromResult(true); } Your current implementation is very inefficient, as it builds the state machine, and also uses a ThreadPool thread … Read more

Properly handling HttpClient exceptions within async / await

As you are using HttpClient, try to use response.EnsureSuccessStatusCode(); Now HttpClient will throw exception when response status is not a success code. try { HttpResponseMessage response = await client.GetAsync(“http://www.ajshdgasjhdgajdhgasjhdgasjdhgasjdhgas.tk/”); response.EnsureSuccessStatusCode(); // Throw if not a success code. // … } catch (HttpRequestException e) { // Handle exception. } ORIGINAL SOURCE OF THE CODE: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

How can I use async/await with SwiftUI in Swift 5.5?

I’m the author of the article you referenced. As discussed in Discover concurrency in SwiftUI, views can make use of the new .task { } and .refreshable { } modifiers to fetch data asynchronously. So you now have the following options to call your async code: func someSyncMethod() { doSomeSyncWork() Task { await methodThatIsAsync() } … Read more

try..catch not catching async/await errors

400/500 is not an error, it’s a response. You’d only get an exception (rejection) when there’s a network problem. When the server answers, you have to check whether it’s good or not: try { let response = await fetch(‘not-a-real-url’) if (!response.ok) // or check for response.status throw new Error(response.statusText); let body = await response.text(); // … Read more

How to make Task.WaitAll() to break if any exception happened?

The following should do it without altering the code of the original tasks (untested): static bool WaitAll(Task[] tasks, int timeout, CancellationToken token) { var cts = CancellationTokenSource.CreateLinkedTokenSource(token); var proxyTasks = tasks.Select(task => task.ContinueWith(t => { if (t.IsFaulted) cts.Cancel(); return t; }, cts.Token, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current).Unwrap()); return Task.WaitAll(proxyTasks.ToArray(), timeout, cts.Token); } Note it only tracks faulted tasks … Read more

How can I Interleave / merge async iterables?

There is no way to write this with a loop statement. async/await code always executes sequentially, to do things concurrently you need to use promise combinators directly. For plain promises, there’s Promise.all, for async iterators there is nothing (yet) so we need to write it on our own: async function* combine(iterable) { const asyncIterators = … Read more

Custom awaitables for dummies

Why would you want a custom awaiter? You can see the compiler’s interpretation of await here. Essentially: var temp = e.GetAwaiter(); if (!temp.IsCompleted) { SAVE_STATE() temp.OnCompleted(&cont); return; cont: RESTORE_STATE() } var i = temp.GetResult(); Edit from comments: OnCompleted should schedule its argument as a continuation of the asynchronous operation.