2016-11-16 22:19:11 8 Comments
I wrote the following node.js file:
var csv = require('csv-parser');
var fs = require('fs')
var Promise = require('bluebird');
var filename = "devices.csv";
var devices;
Promise.all(read_csv_file("devices.csv"), read_csv_file("bugs.csv")).then(function(result) {
console.log(result);
});
function read_csv_file(filename) {
return new Promise(function (resolve, reject) {
var result = []
fs.createReadStream(filename)
.pipe(csv())
.on('data', function (data) {
result.push(data)
}).on('end', function () {
resolve(result);
});
})
}
As you can see, I use Promise.all
in order to wait for both operations of reading the csv files. I don't understand why but when I run the code the line 'console.log(result)'
is not committed.
My second question is I want that the callback function of Promise.all.then()
accepts two different variables, while each one of them is the result of the relevant promise.
Related Questions
Sponsored Content
88 Answered Questions
[SOLVED] How to validate an email address in JavaScript
- 2008-09-05 16:10:11
- pix0r
- 2813964 View
- 4114 Score
- 88 Answer
- Tags: javascript regex validation email email-validation
54 Answered Questions
[SOLVED] Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?
- 2008-09-25 17:54:27
- 2cBGj7vsfp
- 2244916 View
- 3985 Score
- 54 Answer
- Tags: javascript html performance optimization href
41 Answered Questions
[SOLVED] How do I remove a property from a JavaScript object?
- 2008-10-16 10:57:45
- johnstok
- 2086131 View
- 5831 Score
- 41 Answer
- Tags: javascript javascript-objects object-properties
88 Answered Questions
[SOLVED] How do I remove a particular element from an array in JavaScript?
- 2011-04-23 22:17:18
- Walker
- 6151423 View
- 7678 Score
- 88 Answer
- Tags: javascript arrays
38 Answered Questions
[SOLVED] How do you get a timestamp in JavaScript?
- 2008-10-21 09:29:33
- pupeno
- 2730467 View
- 3850 Score
- 38 Answer
- Tags: javascript date datetime timestamp unix-timestamp
59 Answered Questions
[SOLVED] How to replace all occurrences of a string?
- 2009-07-17 17:53:46
- Click Upvote
- 3199799 View
- 4091 Score
- 59 Answer
- Tags: javascript string replace
58 Answered Questions
[SOLVED] How do I include a JavaScript file in another JavaScript file?
- 2009-06-04 11:59:50
- Alec Smart
- 2939148 View
- 4914 Score
- 58 Answer
- Tags: javascript file import include
86 Answered Questions
[SOLVED] How do JavaScript closures work?
- 2008-09-21 14:12:07
- e-satis
- 1391505 View
- 7644 Score
- 86 Answer
- Tags: javascript function variables scope closures
3 Answered Questions
[SOLVED] How to check whether a string contains a substring in JavaScript?
- 2009-11-24 13:04:29
- gramm
- 5822554 View
- 7430 Score
- 3 Answer
- Tags: javascript string substring string-matching
73 Answered Questions
[SOLVED] How can I get query string values in JavaScript?
- 2009-05-23 08:10:48
- Kemal Emin
- 3594874 View
- 2700 Score
- 73 Answer
- Tags: javascript url plugins query-string
2 comments
@nem035 2016-11-16 22:22:30
First question
Promise.all
takes an array of promisesChange:
to (add
[]
around arguments)Second question
The
Promise.all
resolves with an array of results for each of the promises you passed into it.This means you can extract the results into variables like:
You can use ES6+ destructuring to further simplify the code:
@CrazySynthax 2016-11-16 22:25:46
thanks. And what about my second question? Now, result includes all the data from both files. How can get each file's data to a separate array?
@nem035 2016-11-16 22:27:12
@CrazySynthax No problem, check out my edit
@rsp 2016-11-16 22:25:58
Answer to your second question:
If you want the
then
callback to accept two different arguemnts, then you can use Bluebird and itsspread
method. See:Instead of
.then(function (array) { ... })
and having to accessarray[0]
andarray[1]
inside of yourthen
handler you will be able to usespread(function (value1, value2) { ... })
and have both variables named as you want.This is a feature of Bluebird, it's not possible with plain
Promise
.You use Bluebird just like Promise, e.g.:
Of course you don't have to name it
P
but whatever you want.For more examples see the Bluebird Cheatsheets.
@CrazySynthax 2016-11-16 22:29:13
thanks. Isn't there any way to do it without bluebird ?
@rsp 2016-11-16 22:30:55
@CrazySynthax No. A standard Promise has only
then
which gets an array for Promise.all.@CrazySynthax 2016-11-16 22:34:02
and if I use 'spread', how is error handling being committed? I didn't see a catch clause in the link.
@rsp 2016-11-16 23:46:34
@CrazySynthax You use
.spread(...).catch(...)
just like you would.then(...).catch(...)
.@rsp 2016-11-16 23:47:35
@CrazySynthax See the Bluebird cheatsheet