Blog: async/await

JavaScript Promises: Tips, tricks and best practices

January 15, 2016, (updated on April 11, 2017), Software Development

JavaScript Promises are a very powerful tool to avoid the callback nesting hell. The following article describes some tips and tricks when working with Promises. For developers with C# async/await knowledge, I will also show the async/await representations. With TypeScript, you can use this async/await syntax today. Most modern browsers only support JavaScript ES5 which does not provide a native Promise implementation. This is why you need one of the 3rd party Promises frameworks. When choosing a framework, you should check that it follows the Promises/A+ specification or a polyfill. The samples in this article are implemented using the Q […] Read more...

Tags: , , ,

Inside Async/Await: Synchronize an async method or code block

November 5, 2015, (updated on November 10, 2015), 1 comment, Software Development

Have you ever tried to await a task inside a lock() block? It is not possible, because you can only synchronize synchronous code with the lock keyword. However, in today’s .NET development, async/await is used everywhere and the need to synchronize asynchronous code blocks is coming up quite often. This is why I wrote a simple class which can be used to synchronize asynchronous code blocks. As an example for this article, we have a simple synchronous method which is synchronized using a lock() block: private object _lock = new object(); public void Run(int i) { lock (_lock) { Console.WriteLine("Before: […] Read more...

Tags: , , , , ,

Create Task-based method from “legacy” callback method

March 20, 2012, (updated on November 5, 2015), 4 comments, Software Development

In my projects I have a lot of asynchronous methods with a callback as parameter which is called when the operation has completed. I asked myself how to port these methods to support the new async / await keywords and if possible allow me to use the class with older frameworks which do not support this new functionality. The samples in this article use the Http classes from my library project MyToolkit. We start with a simple method called LoadHtml which will call the callback action completed after completion: public void LoadHtml(String url, Action<HttpResponse> completed) { Http.Get(url, completed); } To […] Read more...

Tags: , , , ,

  Page 1 of 1