0
votes

I use node.js.

I use parallel method in async module to control asynchronous.

In each task function, run python code and get result.

But that result wrapped up in the Promise object.

code

const tasks = [
    function(callback) {
        pythonShell.run(options, (err, jsonArray)=> {
            callback(null, jsonArray)
        }
    },
    function(callback) {
        pythonShell.run(options, (err, jsonArray)=> {
            console.log(jsonArray) //1
            callback(null, jsonArray)
        }
    }
]

async.parallel(tasks, (err, results) => {
    if (err) return err.message
    console.log(results)//2
});

first console.log

Promise {
    [{...}, {...}]
}

second console.log

[
    Promise {
        [{...}, {...}]
    },
    Promise {
        [{...}, {...}]
    }
]

I only want json array in Promise { ... }

How can I get it?

1
Try replacing console.log(results) with Promise.all(results).then(console.log)? - somethinghere
Simply use .then(array => { /* Do something with the array of results */ }) - somethinghere

1 Answers

0
votes

use .then((array_of_data_variable) => {

your desired action over here(display to user, save to json etc)

}

this is using the ES6 syntax style.