I'm trying to build a sort
function that should be able to work with and without promises indistictevly.
The function fingerprint should be something like this:
function sort<T>(list: T[], fn: (item: T) => string | number): T[];
function sort<T>(list: Promise<T[]>, fn: (item: T) => string | number): Promise<T[]>;
So the idea is that I can use it regardless if list
is or not a promise while the return type should match the input type.
I've done this kind of constructions with other types (like a map function for both arrays and objects) but promises present an additional challenge as I would need to declare the method as async
in order to plus, doing so, it forces me to always return a promise.
How can I do this? Is this even possible?