Use async await with Array.map

Given the following code: var arr = [1,2,3,4,5]; var results: number[] = await arr.map(async (item): Promise<number> => { await callAsynchronousOperation(item); return item + 1; }); which produces the following error: TS2322: Type ‘Promise<number>[]’ is not assignable to type ‘number[]’. Type ‘Promise<number> is not assignable to type ‘number’. How can I fix it? How can I … Read more

Synchronously waiting for an async operation, and why does Wait() freeze the program here

Preface: I’m looking for an explanation, not just a solution. I already know the solution. Despite having spent several days studying MSDN articles about the Task-based Asynchronous Pattern (TAP), async and await, I’m still a bit confused about some of the finer details. I’m writing a logger for Windows Store Apps, and I want to … Read more

How can I use async/await at the top level?

I have been going over async/await and after going over several articles, I decided to test things myself. However, I can’t seem to wrap my head around why this does not work: async function main() { var value = await Promise.resolve(‘Hey there’); console.log(‘inside: ‘ + value); return value; } var text = main(); console.log(‘outside: ‘ … Read more

What is the difference between asynchronous programming and multithreading?

I thought that they were basically the same thing — writing programs that split tasks between processors (on machines that have 2+ processors). Then I’m reading this, which says: Async methods are intended to be non-blocking operations. An await expression in an async method doesn’t block the current thread while the awaited task is running. … Read more

When correctly use Task.Run and when just async-await

I would like to ask you on your opinion about the correct architecture when to use Task.Run. I am experiencing laggy UI in our WPF .NET 4.5 application (with Caliburn Micro framework). Basically I am doing (very simplified code snippets): public class PageViewModel : IHandle<SomeMessage> { … public async void Handle(SomeMessage message) { ShowLoadingAnimation(); // … Read more

Can constructors be async?

I have a project where I’m trying to populate some data in a constructor: public class ViewModel { public ObservableCollection<TData> Data { get; set; } async public ViewModel() { Data = await GetDataTask(); } public Task<ObservableCollection<TData>> GetDataTask() { Task<ObservableCollection<TData>> task; //Create a task which represents getting the data return task; } } Unfortunately, I’m getting … Read more

How to safely call an async method in C# without await

I have an async method which returns no data: public async Task MyAsyncMethod() { // do some stuff async, don’t return any data } I’m calling this from another method which returns some data: public string GetStringData() { MyAsyncMethod(); // this generates a warning and swallows exceptions return “hello world”; } Calling MyAsyncMethod() without awaiting … Read more

Is Task.Result the same as .GetAwaiter.GetResult()?

I was recently reading some code that uses a lot of async methods, but then sometimes needs to execute them synchronously. The code does: Foo foo = GetFooAsync(…).GetAwaiter().GetResult(); Is this the same as Foo foo = GetFooAsync(…).Result; 7 Answers 7 Task.GetAwaiter().GetResult() is preferred over Task.Wait and Task.Result because it propagates exceptions rather than wrapping them … Read more