async.js

How to synchronize an async map function in javascript

…衆ロ難τιáo~ 提交于 2021-01-29 14:53:24
问题 I have a async map function but want it to execute synchronously because I need the output of the first statement to be used within the same loop. But the map runs asynchronously even with await statements, can you please help understand why this happens. My use case if to insert a record into mongodb if not present and update it if present in a loop. The data exists in the db but find fails within the loop but works outside. My code: const doSomethingAsync = () => { return new Promise

Async function must return a boolean value

时光总嘲笑我的痴心妄想 提交于 2021-01-29 06:31:09
问题 I have a method that I am calling on the onsubmit event in the form tag. So I need a true or false to be returned from the method. I use an API to retrieve data, and according to the response from the API, I return true or false. But because it is an async function thats running, I cant get it right to wait for the response from the API, analyze it and then return my decision. Any ideas on how I could solve this problem function GetPolygonID() { document.getElementById("displayerror")

Javascript - Callback after all nested forEach loops are completed

孤街浪徒 提交于 2020-08-24 13:21:58
问题 I'm sure this is a fairly simple task, but I'm not able to wrap my head around it at this time. I've got a nested set of forEach loops, and I need to have a callback for when all the loops are done running. I'm open to using async.js This is what I'm working with: const scanFiles = function(accounts, cb) { let dirs = ['pending', 'done', 'failed']; let jobs = []; accounts.forEach(function(account) { dirs.forEach(function(dir) { fs.readdir(account + '/' + dir, function(err, files) { files

async.mapLimit with Promise

倾然丶 夕夏残阳落幕 提交于 2020-08-10 23:43:05
问题 I'm trying to use the async module (v3) and especially the async.mapLimit method to submit a limited number of parallel asnychronous requests. This works well with callbacks in the following (simplified) sample code: async = require('async'); async.mapLimit(['1','2','3','4','5'], 3, function(num, callback){ setTimeout(function(){ num = num * 2, console.log(num); callback(null, num); }, 4000); },function(err, results){ console.log(results); }); As result I get the single values and finally the

Choose proper async method for batch processing for max requests/sec

ε祈祈猫儿з 提交于 2020-02-09 01:55:48
问题 I need to perform a cyclic call to some external API with some delay, to prevent from 'User Rate Limit Exceeded' restriction. Google Maps Geocoding API is sensitive to 'req/sec', allowing 10 req/sec. I should make geocoding for hundreds of my contacts, and such delay is required. So, I need have a 10 async geocoding functions with post-delay in 1 sec for each. So, I collect all contacts in array, and then I loop through array in async manner. Generally, I need to have a N simultaneous threads

How to test .promise() methods used in AWS built in methods in jest Node JS

爷,独闯天下 提交于 2020-02-05 05:43:05
问题 I want to unit test this completely the code for my function is given below function.js async function sesSendEmail(message) { var ses = new aws.SES({ apiVersion: "2020-12-01" }); var params = { Source: "abc@gmail.com", Template: "deviceUsageStatisticsEmailTemplate", Destination: { ToAddresses: ["xyz@gmail.com"] }, TemplateData: message, } try { let res = await ses.sendTemplatedEmail(params).promise() console.log(res) } catch (err) { console.log(err) } What i have tried in my tests so far:

Node.js, async module, concurrency

大兔子大兔子 提交于 2020-01-25 12:19:14
问题 Am I best to avoid using more than one instance of an async module feature at a time? My code has three parts to be performed in sequence. I'm using code like var async = require('async'); async.waterfall( [ function(cb) { part1(); }, function(cb) { part2(); }, function(cb) { part3(); } ], function(err, res) { console.log('Done'); } ); part1() has three parts, too. The third of those runs after the first two. My thought was to use function part1(err, res) { async.waterfall( [ function(cb) {

How to convert Step code to Async.JS (Step => waterfall, this.parallel)?

我的梦境 提交于 2020-01-15 12:46:07
问题 A couple years ago I was experimenting with NodeJS and found that the "Step" library cleaned up some of my code rather nicely. When desiring to bring that code up to date, I noticed a few "red flags" on Step. (Not being updated for a couple of years, just 32 commits, etc.) So I looked around and found Async.js, which has more features and active maintenance. Looks nice and general. But I started trying to apply a transformation to use it instead, and might not be taking the right angle. If I

try/catch block not catching async/await error

折月煮酒 提交于 2020-01-14 03:23:07
问题 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;

async.waterfall in a For Loop in Node.js

耗尽温柔 提交于 2020-01-13 03:24:05
问题 When using async.waterfall within a for loop, it appears that the for loop iterates before the nested async.waterfall finishes all its steps. How can this be avoided? for(var i = 0; i < users.length; i++ ) { console.log(i) async.waterfall([ function(callback) { callback(null, 'one', 'two'); }, function(arg1, arg2, callback) { // arg1 now equals 'one' and arg2 now equals 'two' callback(null, 'three'); }, function(arg1, callback) { // arg1 now equals 'three' callback(null, 'done'); } ],