[Solved] One variable returns two seperate array

[ad_1]

Since the two request aren’t linked in any way and are asynchronous, you have to group the data manually once both files have been read.

I usually use util.promisify() to turn fs.readFile() from accepting a callback into it returning a promise, so I can use Promise.all() to create the array once both files have been read:

var fs = require('fs');
var csv = require('csv');
var { promisify } = require( 'util' );

var readFile = promisify( fs.readFile );

Promise
    .all([
        readFile( 'datafile1.csv' ),
        readFile( 'datafile2.csv' )
    ])
    .then( files => files.map( data => {
        var lines = data.trim().split('\n');
        var lastLine = lines.slice(-1)[0];
        var fields = lastLine.split(',');
        var humidity = fields.slice(-1)[0];
        var temperature = fields.slice(-2)[0];
        return parseFloat( temperature );
    }))
    //  Temperatures should equal [24.57083333333333, 20.57083333333333]
    .then( temperatures => console.log( temperatures ))
    .catch( error => {
        //  Handle error.
        console.error( error );
    });

2

[ad_2]

solved One variable returns two seperate array