7

最近我在做一个新项目,这个项目在 nodejs 中使用 JavaScript 回调。现在我们使用KOA,但是当我们尝试使用 ES6 生成器和回调时会出现问题。

//Calback function
function load(callback){
  result = null;
  //Do something with xmla4js and ajax
  callback(result);
  return result;
}

现在在KOA中,我需要调用loadjson并响应客户端,所以我使用下面的代码:

router= require('koa-router');
app = koa();
app.use(router(app));

app.get('load',loadjson);

function *loadJson(){
  var that = this;
  load(function(result){
    that.body = result;
  });
}

但我收到此错误:

_http_outgoing.js:331
throw new Error('Can\'t set headers after they are sent.');
      ^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:331:11)
at Object.module.exports.set (G:\NAP\node_modules\koa\lib\response.js:396:16)
at Object.length (G:\NAP\node_modules\koa\lib\response.js:178:10)
at Object.body (G:\NAP\node_modules\koa\lib\response.js:149:19)
at Object.body (G:\NAP\node_modules\koa\node_modules\delegates\index.js:91:31)
at G:\NAP\Server\OlapServer\index.js:40:19
at G:\NAP\Server\OlapServer\OLAPSchemaProvider.js:1599:9
at _LoadCubes.xmlaRequest.success   (G:\NAP\Server\OlapServer\OLAPSchemaProvider.js:1107:13)
at Object.Xmla._requestSuccess (G:\NAP\node_modules\xmla4js\src\Xmla.js:2113:50)
at Object.ajaxOptions.complete (G:\NAP\node_modules\xmla4js\src\Xmla.js:2024:34)
4

5 回答 5

15

只是为了澄清一些事情,让我们把你的回调写成

//Calback function
function load(callback){
    setTimeout(function() {
        var result = JSON.stringify({ 'my': 'json'});
        callback(/* error: */ null, result);
    }, 500);
}

在 Koa 世界中,这被称为 a thunk,这意味着它是一个异步函数,只接受一个参数:带有原型 (err, res) 的回调。您可以查看https://github.com/visionmedia/node-thunkify以获得更好的解释。

现在你必须用

function *loadJson(){
  this.type = 'application/json';
  this.body = yield load;
}
于 2014-02-27T08:04:13.743 回答
1

这主要是因为 KOA 是基于生成器的,如果你在中间件的顶部它不支持回调。所以它不等待功能完成。最好的解决方案是将您的功能转换为承诺。Promise 与 KOA 配合得很好。

于 2015-07-06T12:30:43.770 回答
0

我在使用braintree(定期回调)和koa时遇到了一个非常相似的问题。根据您的代码,我需要做的唯一更改是加载函数及其调用方式。

router = require('koa-router');
app = koa();
app.use(router(app));

app.get('/load',loadjson);

function *loadJson(){
  this.body = yield load;
}

// Callback function
function load(callback) {
  // Prepare some data with xmla4js and ajax
  whatever_inputs = {...};
  final_method(whatever_inputs, callback);
}

上面 Jerome 和 Evan 的解释是绝对正确的,thunkify看起来是一个适合自动执行它的过程。

于 2015-12-31T06:05:48.417 回答
0

虽然 thunk 是个好主意,但在我看来,aPromise是一种更好的长期方法。许多库已经转向异步的承诺,而不是旧的节点标准callback(err, data),并且它们非常简单地包装任何异步代码来做出承诺。其他开发人员将有使用 Promises 的经验并自然理解您的代码,而大多数开发人员将不得不查找“thunk”是什么。

例如,在这里我将基于尚未承诺的 jsdom 包装在一个承诺中,因此我可以在我的 koa 生成器中生成它。

const jsdom = require('node-jsdom');
const koa = require('koa');
const app = koa();
​
app.use(function *() {
  this.body = yield new Promise((resolve, reject) => jsdom.env({
    url: `http://example.org${this.url}`,
    done(errors, { document }) {
      if (errors) reject(errors.message);
      resolve(`<html>${document.body.outerHTML}</html>`);
    },
  }));
});
​
app.listen(2112);

从语义上讲,promise 和生成器齐头并进,以真正阐明异步代码。生成器可以多次重新输入并产生多个值,而 promise 表示“我保证稍后会为您提供一些数据”。结合起来,你会得到 Koa 最有用的东西之一:产生 Promise 和同步值的能力。

编辑:这是您的原始示例,其中包含返回的 Promise:

const router = require('koa-router');
const { load } = require('some-other-lib');
const app = koa();
app.use(router(app));

app.get('load', loadjson);

function* loadJson() {
  this.body = yield new Promise(resolve => {
    load(result => resolve(result));
  });
}
于 2016-06-15T00:01:19.983 回答
-1

要绕过 Koa 的内置响应处理,您可以显式设置this.respond = false;。如果您想写入原始res对象而不是让 Koa 为您处理响应,请使用此选项。

在调用回调之前,标头已由内置响应处理编写。

于 2014-09-22T10:53:59.863 回答