Is the middleware Synchronize in the koa2 framework or asynchronous?

question 1: why is the middleware in the koa2 framework written in the form of async, rarely in Synchronize mode (that is, without async)? For example,

app.use(async (ctx, next) => {
  const start = new Date()
  await next()
  const ms = new Date() - start
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})

after looking up some materials, I can see that in Ruan Yifeng"s koa tutorial, there is an example of Synchronize writing:

const one = (ctx, next) => {
  console.log(">> one");
  next();
  console.log("<< one");
}

const two = (ctx, next) => {
  console.log(">> two");
  next(); 
  console.log("<< two");
}

const three = (ctx, next) => {
  console.log(">> three");
  next();
  console.log("<< three");
}

app.use(one);
app.use(two);
app.use(three);

/*
output:
>> one
>> two
>> three
<< three
<< two
<< one
*/

there is no async at all. Nguyen only mentioned later: "so far, the middleware in all examples is Synchronize, without asynchronous operations." If there are asynchronous operations (such as reading a database), the middleware must be written as an async function. "

then look for information and find that the next of koa2 middleware returns promise, because the dispatch (i) in the source code returns promise, so I have another question,
question 2: why is it designed to return promise? Can"t you achieve the effect of middleware concatenation without returning promise? Because the execution of middleware is a Synchronize process in understanding, how to understand it when designed asynchronously?

question 3: is there a lot of async function in the code written in koa2 (the company project code is full of async, do you also need async when returning the output json?) ? Are there any scenarios or examples that don"t need to write async?


  1. Asynchronous functions are also functions, except that they return a promise object in which promise concatenation may exist
  2. promise is most suitable for asynchronous operations, which make them look like Synchronize's, otherwise it will be troublesome and the logic is not clear enough.
  3. you can completely ignore the Synchronize function and asynchronous function, which can be accepted. The return value is promise and wait until promise completes
  4. .

1. Because turnover 1 requires await next () , as a middleware, it is difficult to determine whether there is a Synchronize method or an asynchronous method in your next . If you need to wait for its execution to be completed, it is best to deal with it as an asynchronous method, of course, you can not use async/await .
2. Returning Promise is currently the most appropriate way to deal with asynchrony. Without understanding that the execution of middleware is a Synchronize process , executing script is a Synchronize process, but it probably contains asynchronous operations.
3. async function does not mean that there must be an asynchronous method inside it, and the output of json does not require async . It is more likely that an asynchronous method may appear inside the method and want to use await to wait for its execution, and async function returns a Promise .


first of all, you can take a look at the basic knowledge and principles related to generator,async,await, and take a look at Ruan Yifeng's tutorial. Async mainly solves the problem of asynchronous operation, we can not write Promise and callback, can execute an asynchronous method just like calling Synchronize method. Next to answer the question
1, of course, you can not use async and await, even if you have asynchronous operations in the middleware, you can also deal with through promise, but the syntax of async writes more like Synchronize, looks more refreshing
2, because await must be a promise, so next is designed as a promise (so it is recommended that you first look at the principle). You can achieve the cascading effect without using promise, so you have to use callbacks. For the execution of middleware, you have to consider a situation. In a middleware, I need to call an interface, and the interface returns and then goes to the next middleware. This is actually an asynchronous situation
3, or back to the beginning, async is just a solution that makes it easier for us to write asynchronous methods, so you don't have to

.
Menu