Tag: asynchronous

处理asynchronous节点错误的最佳方法

为了捕捉错误,我写了每个函数看起来不好的if-else块。 请build议一个更好的方法来处理asynchronous节点中的错误 async.waterfall([ function(callback){ fnOne.GetOne(req, res,function(err,result) { if(err){ console.error("Controller : fnOne",err); callback(err,null); } else{ var fnOne = result; callback(null, fnOne); } }) }, function(fnOne, callback){ fnTwo.two(fnOne,function(err,result) { if(err) { console.error(err); callback(err,null); } else{ callback(null, context); } }) } ], function (err, result) { if(err){ console.error("Controller waterfall Error" , err); res.send("Error in serving request."); } });

如果使用openSync打开文件,如何获取node.js中的文件描述符

我已经注意到openSync可能是个大问题,当你用openSync打开一个文件时,你不会得到文件描述符。 如果使用asynchronous调用打开,则只会将其作为callback的参数。 问题是你必须有文件描述符来closures文件! 还有一些程序员可能想要对文件做一些其他的事情,你也需要文件描述符。 在fs API中,node.js似乎没有提供一种访问fdvariables的方法,如果您打开使用同步调用,则在asynchronous模式下打开时,会返回callback。 这基本上使同步开放对大多数应用程序不可用。 我真的不希望不得不使用asynchronous文件打开和closures在我的发展,如果我可以避免它,有没有办法获得fdvariables我需要在使用同步打开时成功closures文件?

NodeJS readFile()检索文件名

我遍历包含文件名的数组。 对于他们每个人,我调用readFile() 。 当调用相应的callback函数时,我希望检索传递给readFile()的文件名作为参数。 可以这样做吗? 封闭了一个简短的代码,以更好地解释我的意图。 var fs = require("fs"); var files = ["first.txt", "second.txt"]; for (var index in files) { fs.readFile(files[index], function(err, data) { //var filename = files[index]; // If I am not mistaken, readFile() is asynchronous. Hence, when its // callback is invoked, files[index] may correspond to a different file. // (the index […]

asynchronousJavaScript引发exception没有捕获

基本上,为什么不抓住这个例外? var http = require('http'), options = { host: 'www.crash-boom-bang-please.com', port: 80, method: 'GET' }; try { var req = http.request(options, function(res) { res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); req.on('error', function(e) { throw new Error("Oh noes"); }); req.end(); } catch(_error) { console.log("Caught the error"); } 有人build议这些错误需要用事件发射器或callback(err)来处理(callbackerr,数据签名不是我习惯的) 什么是最好的方式去呢?

在node.js中使用asynchronous瀑布时的“err”参数

我正在尝试执行一系列函数,每个函数都将callback传递给下一个函数。 现在看起来像这样(原谅任何小错误,我正在重写,因为我张贴!): function func1(callback) { callback(null, "stuff"); } function func2(input, callback) { callback(null, "foo" + input); } async.waterfall([func1, func2], function(err, result) { sys.puts(result); }); 我的第一个问题是,我不知道如何优雅地启动这个函数,因为它不能接受input。 我将最终将其包装在当地的一个function中,但这仍然让我感到有些不安。 其次,虽然这样做,但我不知道“错误”的论点是如何起作用的。 如果我尝试将其插入参数列表中,则会以各种方式中断。 我希望能够单独捕获任何函数中的错误 – 或者这是需要的,因为我最后一个callback传递了错误?

在Node.js中并行调用函数

我需要在Node.js中做一些独立的数据库查询。 所有查询执行后,应发送回应。 我的第一个尝试看起来像这样: templateData = {}; model.getA(function(result) { templateData.A = result; model.getB(function(result) { templateData.B = result; model.getC(function(result) { templateData.C = result; response.send('template', templateData); }) }) }); 当然,Node.js中的这种方法并不好,因为所有的函数都是按顺序调用的,我放弃了asynchronous编程模式的优点。 我是Node.js的新手,并不清楚如何在并行调用getA() , getB()和getC()之后立即发送响应。 有没有一些非常简单和常见的方法来实现这一点?

node.js async.series不起作用

这段代码直接从以下示例中获取: https : //github.com/caolan/async#seriestasks-callback var async = require("async"); async.series([ function() { console.log("a"); }, function() { console.log("b"); } ], function(err, results){ console.log(err); console.log(results); }); 但是它不起作用。 打印“a”后停止。 是asynchronous模块的最新版本的一个错误,或者我的用法有一些问题?

asynchronous和Q承诺在nodejs

我在nodejs中使用Q库和asynchronous库。 这是我的代码的一个例子: async.each(items, cb, function(item) { saveItem.then(function(doc) { cb(); }); }, function() { }); saveItem是一个承诺。 当我运行这个,我总是得到cb is undefined ,我想then()没有访问权限。 任何想法如何解决这个问题?

Node.js和postgres LISTEN

我想使用Heroku,PostgreSQL和Node.js,并设置它,以便随时在我的postgres数据库中添加loggingNode.js将该行的内容打印到控制台。 我试图按照这些指示设置它: http://lheurt.blogspot.com/2011/11/listen-to-postgresql-inserts-with.html http://bjorngylling.com/2011-04-13/postgres-listen-notify-with-node-js.html 这里是node.js代码 var pg = require('pg'); conString = '/*my database connection*/'; var client = new pg.Client(conString); client.connect(function(err) { if(err) { return console.error('could not connect to postgres', err); } client.connect(); client.query('LISTEN "loc_update"'); client.on('notification', function(data) { console.log(data.payload); }); }); 这是在postgres数据库上执行的function String function = "CREATE FUNCTION notify_trigger() RETURNS trigger AS $$ " + "DECLARE " […]

在整个摩卡testing之前运行asynchronous代码

我正在寻找一种方法来在整个摩卡testing之前运行asynchronous代码。 下面是一个testing的例子,它使用了一系列参数和期望值,并循环了这个数组中的所有项来产生函数断言。 var assert = require('assert') /* global describe, it*/ var fn = function (value) { return value + ' ' + 'pancake' } var tests = [ { 'arg': 'kitty', 'expect': 'kitty pancake' }, { 'arg': 'doggy', 'expect': 'doggy pancake' }, ] describe('example', function () { tests.forEach(function (test) { it('should return ' + test.expect, […]