Now we can make use of our middleware pattern:
- First, we create an instance:
const instance = new Middleware()
- Then, we define a Middleware that waits 500 milliseconds and sets some value on the instance:
instance.use(function (next) { setTimeout(() => { console.log('first') this.firstMiddlewareLoaded = true next() }, 500)})
Note that we use the function () { } syntax to define the middleware function. Using this syntax ensures that the function gets its own this context (which, in this case, will be the instance). An arrow function, however, inherits the this context from the upper scope.
- We define another Middleware that waits 250 milliseconds and sets some value on the instance:
instance.use(function ...