ES6 Koa.js运行生成器函数完成并异步返回

时间:2015-01-13 20:40:36

标签: javascript node.js generator ecmascript-6 koa

使用koa.js,我想创建一个运行生成器函数的API,该函数在后台运行很长时间,但会立即将令牌发送给用户。

然后,用户可以使用该令牌来检索其作业的状态。

'use strict';
var generateToken = function(){
    //...
};

var processData = function *(data, token) {
    //...
    var a = yield analysis(data);
    console.log(a) // a is undefined
};

app.post('/process_data', validate, function *(next) {
    var token = generateToken();
    var that = this;
    setTimeout(function() {
        for (var i of processData(that.request.body, token)){
            continue;
        }
    });
    this.body = "this should return immediately " + token;
    return next;
});

在setTimeout中运行它,不保存变量'a'。我如何构造它以使processData的运行与正常的收益完全相同?

2 个答案:

答案 0 :(得分:3)

您可能希望长时间运行的进程由作业队列处理,例如Kue

您可以使用http帖子

对作业进行排队

然后使用http get

检查作业

以下是我认为您想要做的大致概述:

var kue       = require('kue'),
    koa       = require('koa'),
    route     = require('koa-router'),
    thunkify  = require('thunkify'),
    parse     = require('co-body'),
    co        = require('co'),
    app       = koa(),
    jobs      = kue.createQueue();

app.use(route(app));

// turn callbacks into thunks for generators
var createJob = thunkify(jobs.create);
var findJob   = thunkify(kue.Job.get);

// Process the job here
jobs.process('longProcess', function(job, done){
    // do work in here
    // call done(err) when completed
    // EDIT: if you want to handle job using generators/yield
    // you could use a library like co
    co(function *(){
        var qs = yield doWork(job.data); 
        done();
      }).error(done);
});

// Queue/Start the Job here
app.post('/jobs', function *(){
  var body = yield parse(this);
  var job = yield createJob('longProcess', body);
  this.body = job.id;
});

// Check Status of job here
app.get('/jobs/:token', function *(){
  var job = yield findJob(this.params.token);
  this.body = job;
  // job.status === 'complete' || ...
});

app.listen(3000); 

答案 1 :(得分:0)

感谢Bergi的解决方案。

app.post('/process_data', validate, function *(next) {
    var token = generateToken();

    co(processData(this.request.body, token));

    this.body = "this should return immediately " + token;
    return next;
});