在 node.js/meteor 中控制异步执行流程 [英] Control asynchronous execution flow in node.js/meteor

查看:28
本文介绍了在 node.js/meteor 中控制异步执行流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有来自大气层的http-methods"包和来自 npm 的node-sqlite3"包的陨石应用程序.考虑流动的代码.

I am using a meteorite application with 'http-methods' package from atmosphere and 'node-sqlite3' package from npm. Consider the flowing code.

var results = null;
HTTP.methods({
    'list': function(data) {
      var sqlite3 = Npm.require('sqlite3').verbose();
      var db = new sqlite3.Database(':memory:');

      db.serialize(function() {
        db.run("CREATE TABLE lorem (info TEXT)");

        var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
        for (var i = 0; i < 10; i++) {
            stmt.run("Ipsum " + i);
        }
        stmt.finalize();

        db.all("SELECT rowid AS id, info FROM lorem", function(err, rows) {
            console.log(rows);
            results = rows;
        });
      });

      console.log("Closing DB");
      db.close();

      console.log("Sending back response");
      console.log(results);

      return results;
    }
  });

这里使用域/列表调用上述函数.我想返回 resultSet 作为对请求的响应.但是返回的是null"而不是它.控制台的输出如下所示.

Here using domain/list the above function is invoked. I want to return the resultSet as response to the request. But instead of it 'null' is been returned. The output from the console is given below.

I20131229-23:50:20.092(1)? Closing DB
I20131229-23:50:20.113(1)? Sending back response
I20131229-23:50:20.117(1)? null
I20131229-23:50:20.172(1)? [ { id: 1, info: 'Ipsum 0' },
I20131229-23:50:20.181(1)?   { id: 2, info: 'Ipsum 1' },
I20131229-23:50:20.182(1)?   { id: 3, info: 'Ipsum 2' },
I20131229-23:50:20.183(1)?   { id: 4, info: 'Ipsum 3' },
I20131229-23:50:20.199(1)?   { id: 5, info: 'Ipsum 4' },
I20131229-23:50:20.199(1)?   { id: 6, info: 'Ipsum 5' },
I20131229-23:50:20.199(1)?   { id: 7, info: 'Ipsum 6' },
I20131229-23:50:20.202(1)?   { id: 8, info: 'Ipsum 7' },
I20131229-23:50:20.202(1)?   { id: 9, info: 'Ipsum 8' },
I20131229-23:50:20.203(1)?   { id: 10, info: 'Ipsum 9' } ]

请提出适当的问题解决方案.

Please suggest a appropriate solution to the problem.

注意:给出的代码是实际代码的同义词.如果您想了解更多详情,请告诉我.

Note: The code given is a synonymous of the actual one. Please let me know if you want to know more details.

推荐答案

您可以通过放置 javascript 的延迟方法来解决此问题,该方法会在指定的时间内暂停执行.你可以停止执行几秒钟,你的问题就会解决.

you can solve this problem by putting a delay method of javascript which halts the execution for specified amount of time. you can stop the execution for few second and your problem will be solved.

这篇关于在 node.js/meteor 中控制异步执行流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆