Tag: asynchronous

node.js中的asynchronous编程是否加速了CPU绑定的任务?

今天早些时候,我回答了这个答案的问题。 在我发布的例子中,我在bcrypt节点模块中使用了一个调用的同步版本。 我select使用同步版本的调用主要是因为我认为它使响应看起来更清洁,但我也不认为这会影响性能,因为bcrypt是CPU和内存密集型而不是I / O绑定。 这是我的理解,节点几乎所有的代码在一个单一的线程像浏览器一样,只使用后台线程的事情,如I / O和数据库访问。 这使我相信,CPU密集型任务仍然会“阻塞”服务器,因为没有其他线程可以卸载工作。 对我的回应的一个评论表明,我的假设是错误的,经过一番研究,我意识到我对node.js如何处理这种事情没有太大的把握。 node.js中的asynchronous编程是否会加速cpu和内存密集型调用? 如果是这样,它是如何做到的?

asynchronousfunction – 等待不等待承诺

我试图学习asynchronous等待。 在这个代码中 – const myFun = () => { let state = false; setTimeout(() => {state = true}, 2000); return new Promise((resolve, reject) => { setTimeout(() => { if(state) { resolve('State is true'); } else { reject('State is false'); } }, 3000); }); } const getResult = async () => { return await myFun(); } […]

用node-postgres监听查询超时?

我有一个Postgresql 9.1数据库上的“文章”表和一个通知每个插入通道的触发器。 我想创build一个node.js脚本来捕获这些插入,并使用Socket.io将通知推送到连接的客户端。 到目前为止,我正在使用node-postgres模块侦听通道,但似乎LISTEN查询在大约10-15秒后超时并停止捕获插入。 当超时发生时,我可以查询一个新的监听,但是我不确定如何正确执行延续。 这是我的postgresql通知过程: CREATE FUNCTION article_insert_notify() RETURNS trigger AS $$ BEGIN NOTIFY "article_watcher"; RETURN NULL; END; $$ LANGUAGE plpgsql; 触发: CREATE TRIGGER article_insert_trigger AFTER INSERT ON article FOR EACH ROW EXECUTE PROCEDURE article_insert_notify(); 和node.js代码: var pg = require ('pg'), pgConnection = "postgres://user:pass@localhost/db" pg.connect(pgConnection, function(err, client) { client.query('LISTEN "article_watcher"'); client.on('notification', function(data) { console.log(data.payload); […]

如何处理依赖于多个其他asynchronous函数的asynchronous函数

我有一个asynchronous函数与另外两个asynchronous函数的结果一起工作。 到目前为止,我所做的是在函数2的callback函数1和函数2中写入函数2 function1(callbackFunction() { function2(callbackFunction() { function3() }) }) 有没有其他办法来处理这个问题。 我通常在客户端和nodeJs中使用JavaScript代码。 我的情况是,对于函数2我不需要从函数1的输出。 换句话说,函数1和函数2是独立的; 但函数3依赖于函数1和函数2。 我希望我的函数2在函数1上独立运行,但函数3依赖于函数1和函数2运行。 有什么事吗? function1(); function2(); when(funtion1.complete && funtion2.complete) { function3(); }

yeoman-generator中的this.async()

我正在学习如何编写一个自动生成器。 我有一个关于下面的代码的问题。 它说通过添加var done = this.async(); 并在稍后的callback中调用方法,我们可以使函数askFor()成为一个asynchronous函数。 有人可以解释为什么吗? askFor: function() { var done = this.async(); // Have Yeoman greet the user. this.log(yosay('Welcome to the marvelous Myblog generator!')); var prompts = [{ name: 'blogName', message: 'What do you want to call your blog?', default: 'myblog' }]; this.prompt(prompts, function(props) { this.blogName = props.blogName; done(); }.bind(this)); } 这是this.async的代码 […]

强制量angular器onPrepare等待asynchronoushttp请求

我的量angular器conf.js, onPrepare函数需要做一个http请求,看起来像, onPrepare: function(done) { request.get('http://pepper/sysid') .end(function(err, resp){ if(err || !resp.ok){ log("there is an error " + err.message) done() }else{ global.sysid = resp.sysid done() } }) 它抛出的错误, done is not a function 有没有其他的方法,我可以强制在我的testing开始执行之前调用onPrepare的callback?

如何解决这个MongoDB / Nodeasynchronous问题?

我有以下代码: // Retrieve var MongoClient = require("mongodb").MongoClient; var accounts = null; var characters = null; // Connect to the db MongoClient.connect("mongodb://localhost:27017/bq", function(err, db) { if(err) { return console.dir(err); } db.createCollection('accounts', function(err, collection) { if(err) { return console.dir(err); } else { accounts = collection; } createAccount("bob","bob"); createAccount("bob","bob"); createAccount("bob","bob"); createAccount("bob","bob"); }); }); function createAccount(email, password) { accounts.findOne({"email":email}, […]

重构嵌套的callback,node.js,async

function indexArticles(callback) { fs.readdir("posts/", function(err, files) { async.map(files, readPost, function(err, markdown) { async.map(markdown, parse, function(err, results) { async.sortBy(results, function(obj, callback) { callback(err, obj.date); }, function(err, sorted) { callback( {"articles": sorted.reverse()} ); }); }); }); }); } 我试图找出如何使这个更漂亮 – 你可以告诉我使用caolan的asynchronous库,但我不确定使用哪个控制stream结构。 看来,如果我使用async.waterfall,例如,会导致更多的代码,每个步骤都必须包装在一个匿名函数中。 例如,这只是瀑布的嵌套版本的前两行: function indexArticles(callback) { async.waterfall([ function(callback) { fs.readdir("posts/", function(err, files) { callback(err, files) }) }, […]

Node.js / express:立即响应客户端请求并在nextTick中继续执行任务

我想将服务器高消耗CPU任务与用户体验分开: ./main.js: var express = require('express'); var Test = require('./resources/test'); var http = require('http'); var main = express(); main.set('port', process.env.PORT || 3000); main.set('views', __dirname + '/views'); main.use(express.logger('dev')); main.use(express.bodyParser()); main.use(main.router); main.get('/resources/test/async', Test.testAsync); main.configure('development', function() { main.use(express.errorHandler()); }); http.createServer(main).listen(main.get('port'), function(){ console.log('Express server app listening on port ' + main.get('port')); }); ./resources/test.js: function Test() {} module.exports = Test; […]

Asyncjs:绕过瀑布链中的函数

我想用asyncjs中的nodejs从一个瀑布函数链中跳过一个函数。 我的代码如下所示: async.waterfall([ function(next){ if(myBool){ next(null); }else{ // Bypass the 2nd function } }, // I want to bypass this method if myBool is false in the 1st function function(next){ }, // Always called function(next){ } ]); 你知道一个正确的方法做这个没有放: if(!myBool){ return next(); } 在我想绕过的function。 谢谢 !