So now that we learned a lot about middleware over the last lectures and we have a great way of debugging
the app, let's go back to our app and let's think about running asynchronous code. In our demo project here,
unlike any burger builder, we have no asynchronous tasks. Of course it's easy to create some,
let's say that when storing a new result, we actually want to simulate that we also stored this on a
server.
Now we won't reach out to a server here because it doesn't matter but we can simply use set timeout to
simulate that
this simply takes a short period of time and we want to update the store after this is completed, let's
say, the same for delete result.
So essentially we want to create asynchronous code or actions here,
now as I said, the reducer function has to run synchronously,
you cannot add set timeout here because set timeout which as you know takes a function
and then the timing,
let's say two seconds, here you can't return the new state in there, it doesn't work,
that's not how javascript works. This function only gets executed after two seconds, the switch statement
will be done by then, you can't return in there.
So typically what you would do, you would return a promise which calls resolve inside of set timeout
after a certain period of time but your reducer just doesn't expect to get a promise.
So there is just no way you can execute asynchronous code in here,
there is a different way though. We can execute asynchronous code with the help of so-called action
creators,
so let's dive into action creators first before we then learn how we can utilize them to execute asynchronous
code.