Michael Bleigh @mbleigh.dev · Aug 21

Is there a better pattern than the snippet below for "async initialization classes" where the constructor needs to kick off async work but I want to "push down" waiting for that async to the methods? I find myself using this pattern frequently but it feels clunky and ugly.

3 likes 3 replies

?

Replies

Zach Leatherman · Aug 21

we need async constructors, yesterday!!!

Justin Fagnani · Aug 21

I always make an async static factory method to wrap up the async work. If you want to be strict, you can pass a construction token from the factory to the constructor and validate it so that the ctor can't be called on its own.

Michael Bleigh · Aug 21

OK this is why you should always check your assumptions...for some reason I thought (a) you can't await a promise multiple times (b) you can't await a promise that's already resolved Neither of these are true, so you can just do this.