Skip to content

co vs async

OnedayLiu edited this page Dec 24, 2017 · 2 revisions

Intro

compare error stack between egg@1(co-base) and egg@2(async-base)

Fixture

// /app/service/test.js
class TestService extends Service {
  echo() {
    throw new Error('this is a error');
  }
}

// {2.x}/app/controller/home.js
class HomeController extends Controller {
  async index() {
    this.ctx.body = await this.ctx.service.test.echo();
  }
}

// {1.x}/app/controller/home.js
class HomeController extends Controller {
  * index() {
    this.ctx.body = yield this.ctx.service.test.echo();
  }
}

Error Stack

  • 1.x is missing controller/home.js stack
  • 1.x had many boring co next/onFulfilled stack
  • 2.x is more straightforward

egg@2 - async

2017-11-22 08:43:46,599 ERROR 98784 [-/::1/-/5ms GET /] nodejs.Error: this is a error
    at TestService.echo (/Users/tz/Workspaces/eggjs/test/showcase/app/service/test.js:7:11)
    at HomeController.index (/Users/tz/Workspaces/eggjs/test/showcase/app/controller/home.js:7:49)
    at Object.callFn (/Users/tz/Workspaces/eggjs/test/showcase/node_modules/_egg-core@4.1.0@egg-core/lib/utils/index.js:44:21)
    at Object.classControllerMiddleware (/Users/tz/Workspaces/eggjs/test/showcase/node_modules/_egg-core@4.1.0@egg-core/lib/loader/mixin/controller.js:82:20)
    at Object.callFn (/Users/tz/Workspaces/eggjs/test/showcase/node_modules/_egg-core@4.1.0@egg-core/lib/utils/index.js:44:21)
    at wrappedController (/Users/tz/Workspaces/eggjs/test/showcase/node_modules/_egg-core@4.1.0@egg-core/lib/utils/router.js:317:18)
    at dispatch (/Users/tz/Workspaces/eggjs/test/showcase/node_modules/_koa-compose@3.2.1@koa-compose/index.js:44:32)
    at next (/Users/tz/Workspaces/eggjs/test/showcase/node_modules/_koa-compose@3.2.1@koa-compose/index.js:45:18)
    at /Users/tz/Workspaces/eggjs/test/showcase/node_modules/_koa-router@7.3.0@koa-router/lib/router.js:345:16
    at dispatch (/Users/tz/Workspaces/eggjs/test/showcase/node_modules/_koa-compose@3.2.1@koa-compose/index.js:44:32)

egg@1 - co yield

2017-11-22 08:43:51,772 ERROR 98685 [-/127.0.0.1/-/6ms GET /] nodejs.Error: this is a error
    at TestService.echo (/Users/tz/Workspaces/boom/egg-1.x/app/service/test.js:7:11)
    at echo.next (<anonymous>)
    at onFulfilled (/Users/tz/Workspaces/boom/egg-1.x/node_modules/_co@4.6.0@co/index.js:65:19)
    at /Users/tz/Workspaces/boom/egg-1.x/node_modules/_co@4.6.0@co/index.js:54:5
    at new Promise (<anonymous>)
    at Object.co (/Users/tz/Workspaces/boom/egg-1.x/node_modules/_co@4.6.0@co/index.js:50:10)
    at Object.toPromise (/Users/tz/Workspaces/boom/egg-1.x/node_modules/_co@4.6.0@co/index.js:118:63)
    at next (/Users/tz/Workspaces/boom/egg-1.x/node_modules/_co@4.6.0@co/index.js:99:29)
    at onFulfilled (/Users/tz/Workspaces/boom/egg-1.x/node_modules/_co@4.6.0@co/index.js:69:7)
    at /Users/tz/Workspaces/boom/egg-1.x/node_modules/_co@4.6.0@co/index.js:54:5
Clone this wiki locally