Tag: async.js

从2个mongodb集合中search后,Nodejs进行单个callback

我有2个系列Stud and Prof。 我有一个函数,把id作为参数,并返回相应的信息,如果id属于任何这个集合。 第一个函数调用:传递属于Prof集合的id 第二个函数调用:传递属于Stud集合的id 预期成果:获得教学成果第一和螺柱结果第二 但是,由于asynchronous性质,我总是得到螺旋结果第一和教授结果第二。无论如何,通过引入一个新的variables或改变查询方式来完成这个任务吗? 任何帮助将不胜感激 var check_user_info = function(userid, callback) { Stud.findOne({ '_id': userid }, function(err, stud) { if (err) throw err if (stud) { callback(stud); } else { Prof.findOne({ '_id': userid }, function(err, prof) { if (err) throw err if (prof) { callback(prof); } else { callback(false); } }) } […]

如何从async.js系列返回结果

我看到的每个async.js例子都是这样的: var async = require(“async”); async.series([ function(callback) { setTimeout(function() { console.log(“Task 1”); callback(null, 1); }, 300); }, function(callback) { setTimeout(function() { console.log(“Task 2”); callback(null, 2); }, 200); }, function(callback) { setTimeout(function() { console.log(“Task 3“); callback(null, 3); }, 100); } ], function(error, results) { console.log(results); }); 最后,他们总是只是console.log结果。 但是,你怎么实际返回结果。 每次我尝试它只是回到未定义。 我需要这样的东西: var async = require('async'); var handler […]

是调用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函数都是如此吗?

Nodejsasynchronous编程 – 为什么需要“asynchronous”模块? 什么是“回拨地狱”/“厄运金字塔”?

NodeJS最大的特点之一就是它与我正在阅读的内容是不asynchronous的,但是作为NodeJS的初学者,如果这个模块已经在本地处理了,为什么会有类似async模块呢? https://www.npmjs.com/package/async 我认为有一个很好的理由,但对我来说并不明显。 是处理callback hell还是Pyramid of Doom ?

Map-Reduce in Node

我有我的array=[{id:"1"}, {id:"2"}, {id:"3"}]并要将其转换为对象: {"1":true, "2", true, "3":true}; 所以我做了以下工作: async.reduce(myArray, {}, function (memo, item, callback) { memo[item.id] = true; callback(null, memo); }, function (err, myObj) { console.log("converted object", myObj); } 它做的工作,但我不知道这是否有意义的performance或毫无意义? 在这里他们build议使用map reduce,但是我还是很困惑,怎样才能用map和reduce来提高性能; 任何帮助表示赞赏。 谢谢

每个简单的asynchronouseachLimit示例会引发范围错误?

我的代码非常简单: async.eachLimit(lines, 5, function iterator(line, complete) { console.log(line) complete(null); }, function done(err) { if(err) throw err; console.log('done yo!'); }); 但是,我不断收到以下错误。 我不了解什么? RangeError: Maximum call stack size exceeded

async.auto:第一个错误后停止整个链

我的印象是, async.auto的行为是,如果其中一个任务返回一个err ,全局callback将被调用这个错误, 所有后续的任务将不会执行。 毕竟,他们为什么呢? 全局callback已被调用,所以错误已被报告。 事实certificate,只有依赖于错误任务的任务才会运行,其余部分将会运行。 'use strict'; var async = require('async'); async.auto({ downloadAudio: function (callback) { console.log("downloading audio…"); setTimeout(function () { console.log("downloaded audio."); callback(); }, 10000); }, downloadScript: function (callback) { console.log("downloading script…"); setTimeout(function () { return callback(new Error("ERROR downloading script!")); }, 1000); }, preprocessAudio: ['downloadAudio', function (callback) { console.log("preprocessing audio…"); setTimeout(function () […]

error handling与嵌套callbackasynchronous瀑布

我在async.waterfall( [] , function() )使用async.parallel。 代码如下: async.waterfall( [ function first(callback) { //some processing callback(); }, function second(result, callback) { //Here i query the db model.findAll() .then(function(success) { async.parallel( [array_of_functions], function done(err, result) { if (err) { callback(err); } } ) }) .catch(function(err) { callback(err); }) } ], function done(err, result) { if (err) { throw […]

在Node.js中使用MongoDB进行扩展,我应该什么时候一次查询所有的ID,并行查询每个ID?

可以说我有一个处理N个用户的脚本。 脚本可以看两种方法之一,假设资源充足,速度会更快吗? 我可以使用Promise.all或async.parallel 。 选项1 function processUsers(userIds) { monog.find({_id: { $in: userIds }, (userDocs) => { const tasks = userDocs.map((userDoc) => processUser(userDoc)) async.parallel(tasks, (err, results) => console.log('finished')) }); } 选项2 function processUsers(userIds) { const tasks = userIds.map((userId) => { mongo.findOne({_id: userId}, (err, userDoc) => { processUser(userDoc); }) }) async.parallel(tasks, (err, results) => console.log('finished')) }

将数组传递给asynchronous库eachSeries – 期望Dictionary <{}>'

我有以下TypeScript代码: const allDescribeBlocks: Array<ITestSuite> = suman.allDescribeBlocks; async.eachSeries(allDescribeBlocks, function (block: ITestSuite, cb: Function) { //…. }, cb); 这将会发出警告: typesITestSuite []的参数不可分配给typesDictionary <{}>的参数。 索引签名在ITestSuite []中缺失。 怎么修? 这是确切的警告: