36,718 questions
3325
votes
35
answers
2.4m
views
Using async/await with a forEach loop
Are there any issues with using async/await in a forEach loop? I'm trying to loop through an array of files and await on the contents of each file.
import fs from 'fs-promise'
async function ...
883
votes
24
answers
340k
views
How do I convert an existing callback API to promises?
I want to work with promises but I have a callback API in a format like:
###1. DOM load or other one time event:
window.onload; // set to callback
...
window.onload = function() {
};
###2. Plain ...
1237
votes
49
answers
2.2m
views
Error: Can't set headers after they are sent to the client
I'm fairly new to Node.js and I am having some issues.
I am using Node.js 4.10 and Express 2.4.3.
When I try to access http://127.0.0.1:8888/auth/facebook, i'll be redirected to http://127.0.0.1:...
241
votes
7
answers
40k
views
When is .then(success, fail) considered an antipattern for promises?
I had a look at the bluebird promise FAQ, in which it mentions that .then(success, fail) is an antipattern. I don't quite understand its explanation as for the try and catch.
What's wrong with the ...
773
votes
39
answers
931k
views
Why doesn't adding CORS headers to an OPTIONS route allow browsers to access my API?
I am trying to support CORS in my Node.js application that uses the Express.js web framework. I have read a Google group discussion about how to handle this, and read a few articles about how CORS ...
201
votes
4
answers
159k
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, ...
215
votes
6
answers
156k
views
async function implicitly returns promise?
I read that async functions marked by the async keyword implicitly return a promise:
async function getVal(){
return await doSomethingAync();
}
var ret = getVal();
console.log(ret);
but that is not ...
176
votes
15
answers
216k
views
setState doesn't update the state immediately [duplicate]
I would like to ask why my state is not changing when I do an onClick event. I've search a while ago that I need to bind the onClick function in constructor but still the state is not updating.
Here's ...
1793
votes
34
answers
3.0m
views
How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)
My version of node is always v0.6.1-pre even after I install brew node and NVM install v0.6.19.
My node version is:
node -v
v0.6.1-pre
NVM says this (after I install a version of node for the first ...
200
votes
5
answers
73k
views
Is it bad practice to have a constructor function return a Promise?
I'm trying to create a constructor for a blogging platform and it has many async operations going on inside. These range from grabbing the posts from directories, parsing them, sending them through ...
5561
votes
23
answers
2.1m
views
What's the difference between tilde(~) and caret(^) in package.json?
After I upgraded to the latest stable node and npm, I tried npm install moment --save. It saves the entry in the package.json with the caret ^ prefix. Previously, it was a tilde ~ prefix.
Why are ...
60
votes
2
answers
43k
views
Updating a Nested Array with MongoDB
I am trying to update a value in the nested array but can't get it to work.
My object is like this
{
"_id": {
"$oid": "1"
},
"array1": [
...
771
votes
30
answers
1.3m
views
How to process POST data in Node.js?
How do you extract form data (form[method="post"]) and file uploads sent from the HTTP POST method in Node.js?
I've read the documentation, googled and found nothing.
function (request, response) {
...
1561
votes
13
answers
530k
views
What is the purpose of Node.js module.exports and how do you use it?
What is the purpose of Node.js module.exports and how do you use it?
I can't seem to find any information on this, but it appears to be a rather important part of Node.js as I often see it in source ...
1517
votes
40
answers
729k
views
How to fix npm throwing error without sudo
I just installed node and npm through the package on nodejs.org, and whenever I try to search or install something with npm, it throws the following error unless I sudo the command. I have a feeling ...