async.js

nodejs Async.each Nested loop Confusion

非 Y 不嫁゛ 提交于 2019-12-08 05:04:16
问题 i want to have two nested for loops async.each(ListA, function(itemA,callback1){ //process itemA async.each(itemA.Children, function(itemAChild,callback1){ //process itemAChild callback1(); }), function(err){ console.log("InnerLoopFinished") } callback(); }),function(err){ console.log("OuterLoopFinished") } console.log("Process Finished") Now i expect an output Like { InnerLoopFinished OuterLoopFinished } according to List Size and process Finsished Bt what i get is Process Finished at First

Using async.waterfall

邮差的信 提交于 2019-12-08 02:01:33
问题 I'm using node.js and the async package. Here's the code I have: async.waterfall( [ function(callback) { var data = getSomeData(); callback(null, data); }, function(data, callback) { someFunctionThatNeedsData(data); callback(null, 'done'); } ], function(err, result) { } ); getSomeData has an asynchronous HTTP request that grabs some data from a web service. I'd like to wait until I get a response, and then return that data and pass it to someFunctionThatNeedsData . What I expected was that

nodejs async: multiple dependant HTTP API calls

做~自己de王妃 提交于 2019-12-07 23:22:14
问题 I'm working on a project that involves making multiple HTTP GET requests to different APIs, each requiring information from the last. I'm trying to avoid nested-callaback-and-counter-hell, and have been trying to get it working with the async module. This is what I need to do: I have an array of 1..n course identifiers ( ['2014/summer/iat/100/d100', '2014/spring/bisc/372/d100'] ). For each course in the array, I need to fetch its course outline via a HTTP GET. The resulting outline looks

try/catch block not catching async/await error

折月煮酒 提交于 2019-12-06 19:36:46
I have a simple timeout function which wraps an async function with a timeout to ensure that it fails after a preset amount of time. The timeout function is as follows: export default async function<T = any>( fn: Promise<T>, ms: number, identifier: string = "" ): Promise<T> { let completed = false; const timer = setTimeout(() => { if (completed === false) { const e = new Error(`Timed out after ${ms}ms [ ${identifier} ]`); e.name = "TimeoutError"; throw e; } }, ms); const results = await fn; completed = true; timer.unref(); return results; } I then use this function in this simple code snippet

How to stop executing waterfall on error in async of node.js?

落花浮王杯 提交于 2019-12-06 11:41:30
I am using async module with waterfall method. async.waterfall([ function(callback) { ... callback(err); }, function(result, callback) { console.log("This function should not be executed"); } ], function(err) { if (err) { next(err); return; } } ); But the second function always execute. How to prevent it? Try adding a return async.waterfall([ function(callback) { ... return callback(err); //note return here }, function(result, callback) { console.log("This function should not be executed"); } ], function(err) { if (err) { next(err); return; } } ); 来源: https://stackoverflow.com/questions

Using async.waterfall

浪子不回头ぞ 提交于 2019-12-06 11:13:32
I'm using node.js and the async package. Here's the code I have: async.waterfall( [ function(callback) { var data = getSomeData(); callback(null, data); }, function(data, callback) { someFunctionThatNeedsData(data); callback(null, 'done'); } ], function(err, result) { } ); getSomeData has an asynchronous HTTP request that grabs some data from a web service. I'd like to wait until I get a response, and then return that data and pass it to someFunctionThatNeedsData . What I expected was that getSomeData -- including the callback inside of it -- would have to complete before moving on to invoke

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

nodejs async: multiple dependant HTTP API calls

空扰寡人 提交于 2019-12-06 04:32:05
I'm working on a project that involves making multiple HTTP GET requests to different APIs, each requiring information from the last. I'm trying to avoid nested-callaback-and-counter-hell, and have been trying to get it working with the async module. This is what I need to do: I have an array of 1..n course identifiers ( ['2014/summer/iat/100/d100', '2014/spring/bisc/372/d100'] ). For each course in the array, I need to fetch its course outline via a HTTP GET. The resulting outline looks something like this: { "info": { "nodePath": "2014/spring/bisc/372/d100", "number": "372", "section": "D100

Node + Sequelize: How to check if item exists before adding? (async confusion)

廉价感情. 提交于 2019-12-04 22:53:54
I am unfortunately new to node and running into some confusion regarding the asynchronous/synchronous execution of node. I am using node, sequelize with sqlite and async.js. I have a series of Articles , each of which has a number of Authors . For each Authors in each Article , I'd like to check if the Author exists. If not, create it. The problem is, on the initial run, duplicate authors are being created, I assume due to asynchronous functionality causing an issue with checking for existence. For example, with the array: authors = ['A. Test', 'B. Test', 'C. Test', 'A. Test'] and the code:

node.js async series function's arguments

…衆ロ難τιáo~ 提交于 2019-12-04 18:09:17
问题 I need to do the code like following: function taskFirst(k, v) { console.log(k, v); } function taskSecond(k, v) { console.log(k, v); } function run() { var g1 = "Something"; var g2 = "Something"; var g3 = "Something"; var g4 = "Something"; async.series( [ taskFirst(g1, g2), taskSecond(g3, g4) ], function(error, result){ } ); } What is the right way to pass custom variables and async.js callback function? 回答1: You could do something like this: function taskFirst(k, v, callback) { console.log(k