zoukankan      html  css  js  c++  java
  • koa 中,中间件异步与同步的相关问题

    同步中间件很容易理解,如以下代码:

     1 const Router = require('koa-router')
     2     , koa = new Router({ prefix: '/koa' })
     3 
     4     , fs = require('fs')
     5 ;
     6 /* 中间件执行顺序 与异步中间件 */
     7 const one = (ctx, next) => {
     8     console.log('>> one');
     9     next();
    10     console.log('<< one');
    11 };
    12 
    13 const two = (ctx, next) => {
    14     console.log('>> two');
    15     next();
    16     console.log('<< two');
    17 };
    18 
    19 const three = (ctx, next) => {
    20     console.log('>> three');
    21     next();
    22     console.log('<< three');
    23 };
    24 
    25 koa['get']('/middleStack', one, two, three, async (ctx) => {
    26     console.log('>> four');
    27     ctx.response.type = 'html';
    28     let aaa = 'aa';
    29     console.log('<< four');
    30     ctx.response.body = aaa;
    31 });

    以上代码在控制台一次打印:

    >> one
    >> two
    >> three
    >> four
    << four
    << three
    << two
    << one

    如果将其中一个中间件改为异步中间件:

    1 koa['get']('/middleStack', one, two, three, async (ctx) => {
    2     console.log('>> four');
    3     ctx.response.type = 'html';
    4     let aaa = await fs.openSync('./public/index.html','r');
    5     console.log('<< four');
    6     ctx.response.body = aaa;
    7 });

    则控制台打印顺序为:

    >> one
    >> two
    >> three
    >> four
    << three
    << two
    << one
    << four

    同步中间件并不会等待异步中间件返回结果才继续 next 之后的代码,若将所有的中间件都改为异步中间件:

     1 /* 中间件执行顺序 与异步中间件 */
     2 const one = async (ctx, next) => {
     3     console.log('>> one');
     4     await next();
     5     console.log('<< one');
     6 };
     7 
     8 const two = async (ctx, next) => {
     9     console.log('>> two');
    10     await next();
    11     console.log('<< two');
    12 };
    13 
    14 const three = async (ctx, next) => {
    15     console.log('>> three');
    16     await next();
    17     console.log('<< three');
    18 };
    19 
    20 koa['get']('/middleStack', one, two, three, async (ctx) => {
    21     console.log('>> four');
    22     ctx.response.type = 'html';
    23     let aaa = await fs.openSync('./public/index.html','r');
    24     console.log('<< four');
    25     ctx.response.body = aaa;
    26 });

    >> one
    >> two
    >> three
    >> four
    << four
    << three
    << two
    << one

    打印顺序又回到所有的中间件为同步中间件的情况。

    总结:在 koa 变成中,尽量将所有的中间件都些为异步中间件,使得执行顺序是可控可理解的。

  • 相关阅读:
    Unity3D读取assetbundle
    Unity3D 发布成exe之后黑屏
    Unity3D优化总结
    Unity3D中中 rect[2] == rt->GetGLWidth() && rect[3] == rt->GetGLHeight()错误的原因及解决方法
    C# mysql 插入数据,中文乱码的解决方法
    WPF Canvas做自动缩放时获取控件的实际高度
    面向对象
    常用模块介绍
    python异常处理,多线程,多进程
    python生成器,递归调用
  • 原文地址:https://www.cnblogs.com/ndos/p/9541817.html
Copyright © 2011-2022 走看看