All the examples and code that 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 use is an async one, the results may surprise you. In order to simplify our work and not have to deal with actual API calls, let's create a fakeAPI(delay, value) function that will just delay a while and then return the given value:
const fakeAPI = (delay, value) =>
new Promise(resolve => setTimeout(() => resolve(value), delay));
Let's also have a function to display what fakeAPI() returns, so we can see that things are working as expected:
const useResult = x => console.log(new Date(), x);
We are using the modern async/await features to simplify our code:
(async () => {
console.log("START...