Tag: asynchronous

从嵌套的callback而不是父函数返回值

我有一个父函数有多个callback,需要将最里面的callback的结果传递给调用这个“父”函数的函数。 由于NodeJS是asynchronous的,我的父function显然总是在callback执行之前返回。 我怎样才能使我的callback回到调用者? 我正在使用的代码示例 – var addNewUser = function(hash,name,number,time,syncTime){ // check here if the user exists or not by querying the phone number first connection.query('SELECT user_id FROM users WHERE user_phone_number ="' +number+'"',function(err,rows,fields){ if(rows[0]) return false; connection.query('INSERT INTO users (authorization_hash,user_name,user_phone_number,user_local_time,is_active,last_synced) VALUES ("'+hash+'","'+name+'","' + number+'","' +time+'","1","'+syncTime+'")',function(err,rows,fields){ if(err) throw err; return true; }); }); } 我希望能够将此callback函数返回给调用者函数。

NodeJS + MongoDB ASYNC结果出现较晚

我有一个简单的函数,它返回一个通道中的连接用户列表,我在初始阶段用一个简单的数组来使用它,但是知道,我想从mongodb数据库中检索一些用户细节,并将其添加到该数组中。 我遇到的问题是返回给出了一个空的结果,我可以在控制台日志中清楚地看到,这是因为查询运行较晚,并且在返回结果之前运行返回。 我知道这是因为asynchronousfunction,我已经尝试与Q和其他人,但我只是简直不能得到它的工作。 🙁 //GET USERS FOR SPECIFIED ROOM function get_room_users( room ){ // create an array to hold all the usernames of the poeple in a specific room var roomusers = new Array(); // get all the clients in 'room1′ var clients = io.sockets.clients( room ); var i = 0; for(var i = 0; […]

在Node.js中运行asynchronous进程

我知道node.js有很多像I / O函数那样运行asynchronous的函数。 我试图运行我以asynchronous方式创build的两个模块,这些模块只使用CPU,而不是I / O请求。 例如,我有这样的事情:我先调用f2() ,但是我想要它,所以如果f1()首先结束,它不会等到f2()完成; 那可能吗? 这只是一个例子,但我会用这个调用另一个js文件。 我试图用process.nextTick()写这个,但我不知道这是否是真正的解决scheme。 function f1(){ for (i=0;i< 100;i++){ } console.log("f1") } function f2(){ for (i=0;i< 100000000000;i++){ } console.log("f2") } f2(); f1();

Node.js – 使用多个查询asynchronous构buildJSON

上周我已经开始编写Node.js了。 我写了代码来生成JSON查询多个表, not sure if this is correct way of coding asynchronously build_actor(post, function(actor){ build_post_object(post, function(post_object){ build_post_attachments(post, function(attachments){ build_post_comments(post, function(comments){ var post_obj = {}; post_obj.actor = actor; post_obj.object = post_object; post_obj.attachments = attachments; post_obj.comments = comments; console.log(JSON.stringify(post_obj)); // generates JSON }); }); }); }); function build_actor(post, callback){ //query callback(actor); } function build_post_object(post, callback){ //query callback(post_object); […]

asynchronous每个系列不会按预期返回

即时通讯使用asynchronous库,特别是每个方法,但我没有得到预期的结果。 function instanceMethod (final_callback) { obj = this; async.parallel([ function (callback) { getTopReplies (obj, function (err, result){ if (err) return callback(err); if (result) { obj.topReplies = result; callback(); } }); } // … theres actually more 2 functions here but // I removed them for the sake of space and // readability, ], function (err){ […]

调用可变数量的函数并行?

使用async.parallel或自定义控制stream, arr = [1, 2, 3, 4, 5, 3, 2, 3, 4, 5] //arr could be any length get_something = function(num, cb){ async_io(num, function (err, results){ cb() }); } 我想在“并行”数组的每个成员上运行get_something()。 当他们都完成了,我想callback调用的结果。

使用node.js在stream中进行asynchronous调用的问题

我在我的stream中进行asynchronous调用时遇到问题。 它看起来像是因为某种原因,如果我有三个stream,中间stream进行asynchronous调用,最终的stream永远不会收到“结束”事件。 我可以用一些简单的stream和超时模拟行为。 var options = {} var streamOne = function(){ return through(function write(row){ this.emit('data', row) }) } var streamTwo = function(){ return through(function write(row){ this.pause() var that = this; setTimeout(function(){ that.emit('data', row) that.resume() }, 1000) }) } options.streams = [new streamOne(), new streamTwo()] 然后我通过这个事件stream 。 var streams = [] //Pass stream one and stream […]

在对象内部调用asynchronouscallback函数

我有这个代码: var async = require("async"); module.exports = function(vars){ return { a: function(){ console.log("a()"); }, b: function(){ var self = this; async.series([ function(callback){ … callback(); }, function(callback){ … callback(); } ], function(){ self.a(); // <——- err }); } } } 然后我打电话给b: var test = require("./test.js")({}); test.b(); 但我得到这个错误: Object #<Object> has no method 'a' 。 为什么? 编辑: […]

如何在callback之前设置variables?

我正在devise一个网页。 我有一个函数,我打电话来获取个人主页所需的所有信息。 代码片段是: exports.getHomePageData = function(userId, cb) { var pageData = {}; pageData.userFullName = dbUtil.findNameByUserId(userId, function(err){ if (err) cb(err); }); pageData.classes = dbUtil.findUserClassesByUserId(userId, function(err){ if (err) cb(err); }); cb(pageData); } 我遇到的问题是,我甚至完成设置元素之前调用cb(pageData) 。 我已经看到人们使用asynchronous库来解决这个问题,但是我想知道是否有其他的方法可以让我做到这一点,而不需要更多的模块。

循环中的node.jsasynchronous函数?

我有一些node.js的问题。 我想要做的是获取“./"+req.user.email中的目录数组,并通过循环find它们的大小并添加一个表格行来输出,你可以在代码中看到。 最后,我不想使用res.send()发送所有的表行。 但是,我得到的唯一的输出是: <tr></tr> 为数组中的每个文件。 看起来,forEach函数并不等待readSizeRecursive。 readSizeRecursive函数是asynchronous的,我相信这是什么导致的问题,但我不知道我怎么能解决这个问题。 任何帮助将不胜感激,我也包括readSizeRecursive函数。 谢谢! var output = ""; fs.readdir("./" + req.user.email, function (err, files) { files.forEach(function(file){ output += "<tr>"; readSizeRecursive("./"+req.user.email+"/"+file, function (err, total){ output += '<td>' + file + '</td><td>' + total + '</td>'; }); output += "</tr>" }); res.send(output) }); readSizeRecursive(): // Function to find the size of […]