ctx.end() 和 next() 有什么区别?

What is the difference between ctx.end() and next()?

我正在尝试找出 connector hooks of loopbacknext()ctx.end() 之间的区别。

另外,为什么ctx.end()函数不能用于其他钩子?

感谢您的帮助!

因为如果 end 函数未定义,它们是相同的。

loopback 数据源 juggler 中有一段代码:

if (context.end === undefined) {
    context.end = callback;
  }

您可以在this line

上查看

所以你用哪个都一样

挖掘一段时间后:

  • 首先,next() 和 ctx.end() 共享相同的签名

  • 如果多个个观察者被绑定到连接器,他们将被一个接一个地调用(按绑定顺序)。

    • 调用 next() 会调用下一个观察者
    • 调用 ctx.end() 结束 "closes" 事件

示例 1(咖啡):

connectorName.connector.observe 'after execute', (ctx, next) ->
    console.log "hook1", new Date()
    wait(3000)
    ctx.end null, ctx, ctx.res.body
connectorName.connector.observe 'after execute', (ctx, next) ->
    console.log "hook2", new Date()
    wait(3000)
    next()

示例 2(咖啡):

connectorName.connector.observe 'after execute', (ctx, next) ->
    console.log "hook1", new Date()
    wait(3000);
    next null, ctx, ctx.res.body
connectorName.connector.observe 'after execute', (ctx, next) ->
    console.log "hook2", new Date()
    wait(3000)
    next()

在第一个示例中,从未调用第二个挂钩。在第二个它通常被调用。

pgConnector.connector.observe 'after execute', (ctx, next) ->
    console.log "hook2", new Date()
    wait(3000);
    ctx.end(null, ctx.res)

pgConnector.connector.observe 'after execute', (ctx, next) ->
    console.log "hook3", new Date()
    wait(3000);
    next()

此处与数据库连接器相同。