AwaitWhat lets you await strings that contain a TimeSpan in natural english (e.g. await "2 seconds").
$ dotnet add package AwaitWhat
Task.Delay takes milliseconds or seconds?new TimeSpan with a billion parameters?AwaitWhat is for you!AwaitWhat is a .NET Standard 2.0 library that lets you await a String after you have installed the NuGet package. Yes, you read that right. Just a normal string. No configuration needed, just hit install. Write the TimeSpan format however you want¹ in the string, and just await it! How are you ever going to top the readability of plain english!?
Warning: If it wasn't clear already, you probably should not use this in production code. Like, you can use it, if you really want to. It works fine. But you probably, most certainly, definitely should not use it. I mean, just look at it. You're awaiting a
String. That's not aTask. Shame on you!
dotnet add package AwaitWhat
String in your project. Enjoy!| Standard | With AwaitWhat |
|---|---|
await Task.Delay(1210) | await "1.21s" |
await Task.Delay(1210) | await "1 second, 210 milliseconds" |
await Task.Delay(1210) | await "1210ms" |
await Task.Delay(1210) | await "0.02016667 MiNUteS" |
| ... | ... |
await Task.Delay(new TimeSpan(1, 23, 9)); | await "1h23m9s" |
await Task.Delay(new TimeSpan(1, 23, 9)); | await "1:23:09" |
await Task.Delay(new TimeSpan(1, 23, 9)); | await "1 hr 23 min 9 sec" |
await Task.Delay(new TimeSpan(1, 23, 9)); | await "1 hour, 23 minutes and 9 seconds" |
| ... | ... |
You get the idea, check TimeSpanParser for all the valid formats.
I got the idea from a wonderful video by the wonderful Nick Chapsas. Basically Microsoft has some weird internal compiler wizardry that is exposed and I used that to add an awaiter to String. Just watch the video 👉👈
¹ Okay, not however you want. If you mess it up too much you will get an ArgumentException. Like, don't await your email address, because I have no idea what you want me to do with that. We currently rely fully on the wonderful TimeSpanParser package, so go complain there if you don't like it give them some love!! :)