Tag: asynchronous

为什么async.map函数与原生fs.stat函数一起工作?

async.map(['file1','file2','file3'], fs.stat, function(err, results){ // results is now an array of stats for each file }); 根据文档 ,第二个参数是: iterator(item,callback) – 应用于数组中每个项目的函数。 精细。 迭代器传递一个callback函数(err,transformed),一旦它完成了一个错误(可以是null)和一个转换后的项目,就必须调用它。 我认为fs.stat不符合这一点,我会说这不应该工作。 它应该是这样的: async.map(['file1','file2','file3'], function (file, complete) { fs.stat(file, function (err, stat) { complete(err, stat) }); }, function(err, results){ // results is now an array of stats for each file } );

是调用Node.js的async.parallel()同步吗?

我正在看看Node.js的async模块来解决一个问题。 我已经实施了一个小testing: var async = require("async"); function print(val) { console.log(val); } async.parallel([ function(cb){ print(1); cb(null); }, function(cb){ print(2); cb(null); }, function(cb){ print(3); cb(null); }, function(cb){ print(4); cb(null); }, function(cb){ print(5); cb(null); } ], function(err) { if ( err ) { console.error(err); return; } console.log("Done!"); } ); console.log("Trulu"); 我可以确定在调用async.parallel之前,Trulu日志调用将永远不会被调用吗? 换句话说,在Trulu日志被调用之前调用的所有函数和最终callback函数都是如此吗?

在闭包中调用父函数

我有一个非常非常困难的时间试图在一个asynchronous肥皂请求中调用一个对象函数。 基本上归结为: function Thing(request, response) { this.foo = function() { console.log("this is foo"); } this.doThing = function() { // Get SOAP args args = { foo: this.request.cookies.foo }; // From the SOAP npm package library soap.createClient(function(err, client) { client.doSomething(args, function(err, result) { // Somehow call foo(); <— CAN'T FIND A WAY TO DO THIS }); […]

Node.jsasynchronous用例

最近,我一直在开发Web应用程序,我意识到我没有使用asynchronous属性。 因此,我结束了很多嵌套的callback。 例如,如果用户想通过特定的API从服务器获取文件,我将得到类似于此的代码, db.query(<select list of permitted files_names>, function(err, filenames) { async.each(file_names, function(name, next) { //open each file to put into array }); }) 这段代码需要查询数据库,以便在asynchronous循环之前获取文件名列表,并将每个文件内容放入一个数组中。 最后它会将完成的数组返回给客户端。 使用嵌套的callback和asynchronous库,这个代码的行为就像是一个同步代码。 names = db.querySync(//select list of permitted files_names); for(name in names) { //open each file to put into array } 我更喜欢写这样的同步代码,因为它很整洁。 我的用例可能有点奇怪,但我的API的大部分行为以类似的方式,这让我想为什么我甚至需要asynchronousfunction? 如果这两个编码在performance上有差异,有谁能指教我? 如何使用非阻塞属性来提高此用例的性能?

不能从asynchronous承诺执行器函数中抛出错误

我一直在试图理解为什么下面的代码没有抓住throw 。 如果new Promise(async (resolve, …部分中删除async关键字new Promise(async (resolve, …那么它工作正常,所以它与Promise执行程序是一个asynchronous函数的事实。 (async function() { try { await fn(); } catch(e) { console.log("CAUGHT fn error –>",e) } })(); function fn() { return new Promise(async (resolve, reject) => { // … throw new Error("<<fn error>>"); // … }); } 在这里 , 这里和这里的答案重复“如果你在任何其他asynchronouscallback,你必须使用reject ”,但通过“asynchronous”,他们不是指async函数,所以我不认为他们的解释适用这里(如果他们这样做,我不知道如何)。 如果不是throw我们使用reject ,上面的代码工作正常。 我想从根本上理解为什么throw在这里不起作用。 谢谢!

在Node.js中,setTimeout等待时间过长

我想我有一个误解的是什么setTimeout完全在JavaScript中。 我有这个脚本: function recursiveFibonacci(n) { if ( n === 0 ) { return 1; } else if ( n === 1 ) { return 1; } else { return recursiveFibonacci(n-1) + recursiveFibonacci(n-2); } } setTimeout( () => {console.log("one second");}, 1000); console.log(recursiveFibonacci(42)); 我期望发生的事情是,recursion斐波纳契开始突破斐波那契数列中的第43个值。 我的电脑需要4秒左右的时间。 所以,工作1秒后,评估将会中断,控制台将logging: one second 然后约3秒钟后login: 433494437 相反,在4秒后,控制台会logging: 433494437 one second 一次全部。 这是为什么? 我怎样才能让setTimeout工作? […]

node.js中的asynchronous等待在节点v8.1.0中不起作用

我正在尝试刷新rediscaching数据库,并在响应中返回状态。 但在清除caching之前,它会返回响应。 在下面的代码中,console.log将始终打印未定义,因为flushRedisDB未被等待。 我的节点版本是v8.1.0 myfile.js async function flushRedisapi(request, response) { try { var value = await redisModules.flushRedisDB(); console.log("the value is : " + value); if(value) response.status(200).send({status : 'Redis Cache Cleared'}); else response.status(400).send({status : "Redis Cache could not be flushed"}); } catch (error) { response.status(400).send({status : "Redis Cache could not be flushed"}); } } redismodule.js var […]

asynchronous等待一个条件被满足

在我的代码中,我有三个variables,它们的值依赖于外部函数调用,它们可以在任何时候以任何顺序设置为true,所以它们更像标志。 我只需要一个函数,只要这三个variables设置为true。 我怎样才能以asynchronous的方式执行一个等待这三个variables为真,而不阻塞我的服务器? (我不想被引用到外部库)

nodejs使用callback和事件

我是nodejs编程的新手。 所以请耐心等待 我有两个nodejs模块。 一次传递消息到另一个nodejs模块。 第二个处理它并将结果传递回第一个模块。 方法1 第一个模块 : secondModule.callFunction(message, function(data){ //deal with the return message from the second module }) : 第二个模块 : function callfunction(message, callback){ //asynchornous functions for processing callback(data); } : 方法2 同样的事情,但在第二个模块中使用事件发射器完成 第一个模块 : secondModule.callFunction(message){ }) secondModule.on('done_event', function(data){ //deal with the reply }); : 第二个模块(使用事件发射器) : function callFunction(message){ //asynchornous functions for processing […]

我可以在Nodejs中写一个真正的asynchronouscallback吗?

这是读取文件的一个正常示例: var fs = require('fs'); fs.readFile('./gparted-live-0.18.0-2-i486.iso', function (err, data) { console.log(data.length); }); console.log('All done.'); 上面的代码输出: All done. 187695104 而这是我自己的callback版本,我希望它可以像上面的文件阅读代码asynchronous,但它不是: var f = function(cb) { cb(); }; f(function() { var i = 0; // Do some very long job. while(++i < (1<<30)) {} console.log('Cb comes back.') }); console.log('All done.'); 上面的代码输出: Cb comes back. All done. 到目前为止,很明显在文件读取代码的第一个版本中, […]