Working with async functions
All the examples and code we studied in the previous sections were meant to be used with common functions, specifically meaning not async
ones. When you want to do mapping, filtering, reducing, and so on, but the function you are using is an async
one, the results may surprise you. To simplify our work and not deal with actual API calls, let’s create a fakeAPI(delay, value)
function that will delay a while before returning the given value:
// async.ts const fakeAPI = <T>(delay: number, value: T): Promise<T> => new Promise((resolve) => setTimeout(() => resolve(value), delay) );
Let’s also have a function to display what fakeAPI()
returns so that we can see that things are working as expected:
// continued... const useResult = (x: any): void => console.log(new Date(), x);
We are using the modern async
and await
features from ES2017 to simplify our...