Is it an anti-pattern to use async/await inside of a new Promise() constructor?

I agree with the answers given above and still, sometimes it’s neater to have async inside your promise, especially if you want to chain several operations returning promises and avoid the then().then() hell. I would consider using something like this in that situation: const operation1 = Promise.resolve(5) const operation2 = Promise.resolve(15) const publishResult = () … Read more

What is the purpose of “return await” in C#?

There is one sneaky case when return in normal method and return await in async method behave differently: when combined with using (or, more generally, any return await in a try block). Consider these two versions of a method: Task<SomeResult> DoSomethingAsync() { using (var foo = new Foo()) { return foo.DoAnotherThingAsync(); } } async Task<SomeResult> … Read more

How and when to use ‘async’ and ‘await’

When using async and await the compiler generates a state machine in the background. Here’s an example on which I hope I can explain some of the high-level details that are going on: public async Task MyMethodAsync() { Task<int> longRunningTask = LongRunningOperationAsync(); // independent work which doesn’t need the result of LongRunningOperationAsync can be done … Read more

tech