node-async

Using stream as input to an async queue in Node.js, how to make sure that queue.drain is called only once

人盡茶涼 提交于 2019-12-10 11:38:40
问题 I will be reading a series of inputs from a stream, and perform a HTTP GET request per input. To avoid creating too many connections at a time, I am using async.queue to queue up these inputs. After all inputs are read ( end emitted to the stream), I would like to collect previous results and generate an overview. I am currently using queue.drain for this purpose. But queue.drain may be called multiple times in my case, since the process could be blocked on input and the queue will be empty

async.apply inside async.waterfall

孤人 提交于 2019-12-08 08:10:30
问题 I have the following snippet of code async.waterfall([ // Read directory async.apply(fs.readdir, '../testdata'), // Load data from each file function(files, callback) { async.each(files, loadDataFromFile, callback); } ], function(err) { if (err) { api.logger.error('Error while inserting test data', err); } next(); }); Is there a way I could replace this piece: function(files, callback) { async.each(files, loadDataFromFile, callback); } with just a function? Like I did above, using async.apply

Using async module to fire a callback once all files are read

谁说胖子不能爱 提交于 2019-12-06 05:11:14
问题 I'm using caolan's 'async' module to open an array of filenames (in this case, template file names). Per the documentation, I'm using async.forEach(),so I can fire a callback once all operations have completed. A simple test case is: var async = require('async') var fs = require('fs') file_names = ['one','two','three'] // all these files actually exist async.forEach(file_names, function(file_name) { console.log(file_name) fs.readFile(file_name, function(error, data) { if ( error) { console

Node.js/Async - How to avoid callback hell with async?

回眸只為那壹抹淺笑 提交于 2019-12-04 00:52:05
问题 I'm new to Node.Js and JavaScript web development on the backend. I see that callbacks inside callbacks could be a pain and there are modules to avoid that. One of these modules is async, https://github.com/caolan/async I've read the documentation but it is hard to start and understand how to do it. For example, I've this function "check_aut_user", How can I convert this code using async? function check_auth_user(username, password, done) { var client = new pg.Client("pg://user:pass@127.0.0.1

async.js each get index in iterator

流过昼夜 提交于 2019-12-03 06:30:53
问题 I'm using caolan's async.js library, specifically the .each method. How do you get access to the index in the iterator? async.each(ary, function(element, callback){ //do stuff here for each element in ary //how do I get access to the index? }, function(err) { //final callback here }) 回答1: You can use async.forEachOf - it calls its iterator callback with the index as its second argument. > async.forEachOf(['a', 'b', 'c'], function () {console.log(arguments)}); { '0': 'a', '1': 0, '2':

Node.js/Async - How to avoid callback hell with async?

偶尔善良 提交于 2019-12-01 04:17:11
I'm new to Node.Js and JavaScript web development on the backend. I see that callbacks inside callbacks could be a pain and there are modules to avoid that. One of these modules is async, https://github.com/caolan/async I've read the documentation but it is hard to start and understand how to do it. For example, I've this function "check_aut_user", How can I convert this code using async? function check_auth_user(username, password, done) { var client = new pg.Client("pg://user:pass@127.0.0.1/database"); client.connect(function(err) { // If not get the connection if(err) { return console.error

Cannot use “map” function within async module

最后都变了- 提交于 2019-12-01 01:48:35
I am using node.js "async" module and need to use the "map" method. Basically I have an array that contains other arrays. The inner arrays contains 2 elements, a type and an image filename. var arr0 = []; var arr1 = ["type1", "image1.jpg"]; jsonArr.push(obj1); var arr2 = ["type2", "image2.jpg"]; jsonArr.push(obj2); For each inner array, I want to get the base64 encoding of the image identified by the filename and add this encoding string as the third element of the array. I'm doing something like this: var fs = require("fs"); var async = require("async"); function getImageEncoding(arr,

Asynchronous http calls with nodeJS

旧城冷巷雨未停 提交于 2019-11-30 22:13:59
I would like to launch asynchronous http calls on my server node, i saw the async node module and i guess the async.parallel enables us to do that. The documented example is pretty clear, but i don't know how i could manage multiple http calls. I tried the example bellow but it doesn't even launch the http calls: var http = require('http'); var Calls = []; Calls.push(function(callback) { // First call http.get('http://127.0.0.1:3002/first' callback); }); Calls.push(function(callback) { // Second call http.get('http://127.0.0.1:3002/second' callback); }); var async = require('async'); async

What is the difference between async.waterfall and async.series

雨燕双飞 提交于 2019-11-29 19:29:16
The nodejs async module: https://github.com/caolan/async provides 2 similar methods, async.waterfall and async.series . What is the difference between them? It appears that async.waterfall allows each function to pass its results on to the next function, while async.series passes all results to the final callback. At a higher level, async.waterfall would be for a data pipeline ("given 2, multiply it by 3, add 2, and divide by 17"), while async.series would be for discrete tasks that must be performed in order, but are otherwise separate. Both functions pass the return value, of every function

What is the difference between async.waterfall and async.series

自作多情 提交于 2019-11-28 15:00:25
问题 The nodejs async module: https://github.com/caolan/async provides 2 similar methods, async.waterfall and async.series . What is the difference between them? 回答1: It appears that async.waterfall allows each function to pass its results on to the next function, while async.series passes all results to the final callback. At a higher level, async.waterfall would be for a data pipeline ("given 2, multiply it by 3, add 2, and divide by 17"), while async.series would be for discrete tasks that must