All Questions
Tagged with node.js asynchronous
7,196
questions
884
votes
12
answers
557k
views
Call async/await functions in parallel
As far as I understand, in ES7/ES2016 putting multiple await's in code will work similar to chaining .then() with promises, meaning that they will execute one after the other rather than in parallel. ...
443
votes
12
answers
367k
views
JavaScript, Node.js: is Array.forEach asynchronous?
I have a question regarding the native Array.forEach implementation of JavaScript: Does it behave asynchronously?
For example, if I call:
[many many elements].forEach(function () {lots of work to do})...
299
votes
13
answers
413k
views
Callback after all asynchronous forEach callbacks are completed
As the title suggests. How do I do this?
I want to call whenAllDone() after the forEach-loop has gone through each element and done some asynchronous processing.
[1, 2, 3].forEach(
function(item, ...
285
votes
13
answers
288k
views
How to read file with async/await properly?
I cannot figure out how async/await works. I slightly understand it but I can't make it work.
function loadMonoCounter() {
fs.readFileSync("monolitic.txt", "binary", async ...
262
votes
7
answers
205k
views
How to use the 'main' parameter in package.json?
I have done quite some search already. However, still having doubts about the 'main' parameter in the package.json of a Node project.
How would filling in this field help? Asking in another way, can ...
209
votes
10
answers
207k
views
What is the difference between synchronous and asynchronous programming (in node.js)
I've been reading nodebeginner
And I came across the following two pieces of code.
The first one:
var result = database.query("SELECT * FROM hugetable");
console.log("Hello World");
The ...
188
votes
5
answers
148k
views
Is it an anti-pattern to use async/await inside of a new Promise() constructor?
I'm using the async.eachLimit function to control the maximum number of operations at a time.
const { eachLimit } = require("async");
function myFunction() {
return new Promise(async (resolve, ...
184
votes
3
answers
320k
views
How to return values from async functions using async-await from function? [duplicate]
How can I return the value from an async function?
I tried to like this
const axios = require('axios');
async function getData() {
const data = await axios.get('https://jsonplaceholder.typicode....
164
votes
23
answers
64k
views
How to avoid long nesting of asynchronous functions in Node.js
I want to make a page that displays some data from a DB, so I have created some functions that get that data from my DB. I'm just a newbie in Node.js, so as far as I understand, if I want to use all ...
162
votes
4
answers
308k
views
Node JS Promise.all and forEach
I have an array like structure that exposes async methods. The async method calls return array structures that in turn expose more async methods. I am creating another JSON object to store values ...
154
votes
2
answers
104k
views
What is non-blocking or asynchronous I/O in Node.js?
In the context of Server Side Javascript engines, what is non-blocking I/O or asynchronous I/O? I see this being mentioned as an advantage over Java server side implementations.
154
votes
4
answers
59k
views
Understanding the Event Loop
I am thinking about it and this is what I came up with:
Let's see this code below:
console.clear();
console.log("a");
setTimeout(function(){console.log("b");},1000);
console.log(&...
145
votes
9
answers
158k
views
How to wrap async function calls into a sync function in Node.js or Javascript?
Suppose you maintain a library that exposes a function getData. Your users call it to get actual data:
var output = getData();
Under the hood data is saved in a file so you implemented getData using ...
141
votes
14
answers
157k
views
Best way to call an asynchronous function within map?
I'm mapping over an array and for one of the return values of the new object, I need to make an asynchronous call.
var firebaseData = teachers.map(function(teacher) {
return {
name: teacher....
140
votes
10
answers
283k
views
nodeJs callbacks simple example
can any one give me a a simple example of nodeJs callbacks, I have already searched for the same on many websites but not able to understand it properly, Please give me a simple example.
getDbFiles(...
139
votes
7
answers
234k
views
SyntaxError: Unexpected token function - Async Await Nodejs
I was experimenting on using Node version 6.2.1 with some of my code. Had plans to migrate most of the hyper-callback oriented codes to something that looks cleaner and maybe performs better.
I have ...
132
votes
12
answers
195k
views
fs.writeFile in a promise, asynchronous-synchronous stuff
I need some help with my code. I'm new at Node.js and have a lot of trouble with it.
What I'm trying to do:
Fetch a .txt with Amazon products (ASINs) ;
Fetch all products using the amazon-product-...
119
votes
4
answers
34k
views
What is the difference between async.waterfall and async.series
The nodejs async module: https://github.com/caolan/async provides 2 similar methods, async.waterfall and async.series.
What is the difference between them?
114
votes
6
answers
110k
views
How to write asynchronous functions for Node.js
I've tried to research on how exactly asynchronous functions should be written. After a lot of plowing through a lot of documentation, it's still unclear to me.
How do I write asynchronous functions ...
112
votes
8
answers
185k
views
Simplest way to wait some asynchronous tasks complete, in Javascript?
I want to drop some mongodb collections, but that's an asynchronous task. The code will be:
var mongoose = require('mongoose');
mongoose.connect('mongo://localhost/xxx');
var conn = mongoose....
109
votes
6
answers
108k
views
Asynchronous nodejs module exports
I was wondering what the best approach is for configuring a module export. "async.function" in the example below could be a FS or HTTP request, simplified for the sake of the example:
Here's example ...
106
votes
12
answers
163k
views
How to create threads in nodejs
Is there any way to create threads for running multiple methods at a time?
That way, if any method fails in between all the other threads should be killed.
103
votes
5
answers
59k
views
is node.js' console.log asynchronous?
Are console.log/debug/warn/error in node.js asynchrounous? I mean will javascript code execution halt till the stuff is printed on screen or will it print at a later stage?
Also, I am interested in ...
98
votes
3
answers
115k
views
How to handle the if-else in promise then?
In some case, when I get a return value from a promise object, I need to start two different then() precesses depend on the value's condition, like:
promise().then(function(value){
if(//true) {
...
94
votes
1
answer
54k
views
Nodejs vs SignalR: why do we need server-side javascript?
Since I've known about Node.js, I've always been a fan of it. But today I found about SignalR, which provides an alternative asynchronous - scalable - realtime model for ASP.NET.
As far as I know, ...
88
votes
8
answers
94k
views
When to close MongoDB database connection in Nodejs
Working with Nodejs and MongoDB through Node MongoDB native driver. Need to retrieve some documents, and make modification, then save them right back. This is an example:
db.open(function (err, db) {
...
87
votes
13
answers
125k
views
using async await and .then together
Is there any harm in using async/await and .then().catch() together such as:
async apiCall(params) {
var results = await this.anotherCall()
.then(results => {
//do any results ...
83
votes
5
answers
144k
views
How to sleep the thread in node.js without affecting other threads?
As per Understanding the node.js event loop, node.js supports a single thread model. That means if I make multiple requests to a node.js server, it won't spawn a new thread for each request but will ...
76
votes
10
answers
80k
views
Throttle and queue up API requests due to per second cap
I'm use mikeal/request to make API calls. One of the API's I use most frequently (the Shopify API). Recently put out a new call limit, I'm seeing errors like:
Exceeded 6.0 calls per second for api ...
75
votes
3
answers
81k
views
'await Unexpected identifier' on Node.js 7.5
I am experimenting with the await keyword in Node.js. I have this test script:
"use strict";
function x() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve({...
75
votes
3
answers
23k
views
How does Asynchronous programming work in a single threaded programming model?
I was going through the details of node.jsand came to know that, It supports asynchronous programming though essentially it provides a single threaded model.
How is asynchronous programming handled ...
75
votes
6
answers
153k
views
async function - await not waiting for promise
I'm trying to learn async-await. In this code -
const myFun = () => {
let state = false;
setTimeout(() => {state = true}, 2000);
return new Promise((resolve, reject) => {
...
70
votes
5
answers
54k
views
How to handle errors from setTimeout in JavaScript?
Simple question about try/catch for function in setTimeout
try {
setTimeout(function () {
throw new Error('error!');
}, 300)
} catch (e) {
console.log('eeee!')
console.log(e)
}
...
70
votes
5
answers
79k
views
How to wait for a stream to finish piping? (Nodejs)
I have a for loop array of promises, so I used Promise.all to go through them and called then afterwards.
let promises = [];
promises.push(promise1);
promises.push(promise2);
promises.push(promise3);
...
69
votes
5
answers
125k
views
Why await is not working for node request module?
I'm new to nodejs. I’m not seeing the response in ex 1, but i see in ex 2. Why? Await works for me in other places, using babel.
Ex 1
let res = await request(url)
console.log(res);
console.log(...
63
votes
7
answers
120k
views
Proper request with async/await in Node.JS
In my program I make async call for my function from another API module:
var info = await api.MyRequest(value);
Module code:
var request = require("request")
module.exports.MyRequest = async ...
62
votes
9
answers
215k
views
Looping through JSON with node.js
I have a JSON file which I need to iterate over, as shown below...
{
"device_id": "8020",
"data": [{
"Timestamp": "04-29-11 05:22:39 pm",
"Start_Value": 0.02,
"...
60
votes
2
answers
121k
views
JavaScript wait for asynchronous function in if statement [duplicate]
I have a function inside an if statement
isLoggedin() has an async call.
router.get('/', function(req, res, next) {
if(req.isLoggedin()){ <- never returns true
console.log('...
58
votes
3
answers
46k
views
Why is node.js asynchronous?
Nobody has actually asked this (from all the 'suggestions' I'm getting and also from searching before I asked here).
So why is node.js asynchronous?
From what I have deduced after some research:
...
57
votes
2
answers
24k
views
Reactive Programming - RxJS vs EventEmitter in Node.js
Recently I've started looking at RxJS and RxJava(from Netflix) libraries which work on the concept of Reactive Programming.
Node.js works on the basis of event loops, which provides you all the ...
57
votes
13
answers
14k
views
Is a non-blocking, single-threaded, asynchronous web server (like Node.js) possible in .NET?
I was looking at this question, looking for a way to create a single-threaded, event-based nonblocking asynchronous web server in .NET.
This answer looked promising at first, by claiming that the ...
57
votes
4
answers
17k
views
node.js async libs
There are a ton of libraries that help with fixing the layers of callback syndrome.
In fact, there's too many, which one do i use?
57
votes
2
answers
98k
views
Node.js request module getting ETIMEDOUT and ESOCKETTIMEDOUT
I'm crawling a lot of links with the request module in parallel with combination of the async module.
I'm noticing alot of ETIMEDOUT and ESOCKETTIMEDOUT errors although the links are reachable and ...
56
votes
4
answers
18k
views
Node.js vs Async/await in .net
Can someone explain/ redirect me, what is the difference between Node.js's async model(non blocking thread) vs any other language for example c#'s asynchronous way of handling the I/O. This looks to ...
53
votes
2
answers
76k
views
Node.js - Using the async lib - async.foreach with object
I am using the node async lib - https://github.com/caolan/async#forEach and would like to iterate through an object and print out its index key. Once complete I would like execute a callback.
Here is ...
53
votes
7
answers
24k
views
Asynchronous constructor
How can I best handle a situation like the following?
I have a constructor that takes a while to complete.
var Element = function Element(name){
this.name = name;
this.nucleus = {};
this....
52
votes
2
answers
18k
views
JavaScript asynchronous programming: promises vs generators
Promises and generators allow you to write asynchronous code. I do not understand why both of these mechanisms are introduced in ECMA script 6. When is it best to use the promises, and when the ...
52
votes
5
answers
16k
views
Are all javascript callbacks asynchronous? If not, how do I know which are?
I'm curious as to whether all javascript callbacks are asynchronous, or whether that is the case only in certain situations. Also, I'm sure what makes javascript code asynchronous (or ways to use ...
49
votes
5
answers
30k
views
Limiting asynchronous calls in Node.js
I've got a Node.js app that gets a list of file locally and uploads them to a server. This list could contain thousands of files.
for (var i = 0; i < files.length; i++) {
upload_file(files[i]);...
49
votes
2
answers
29k
views
Does async/await blocks event loop? [duplicate]
I was reading Don't Block the Event Loop from the Node.js guide. There was a line saying:
You should make sure you never block the Event Loop. In other words,
each of your JavaScript callbacks ...