V
V
Vitaly Kuznetsov2015-12-08 20:52:08
Node.js
Vitaly Kuznetsov, 2015-12-08 20:52:08

Is it possible to asynchronously do actions on array elements?

What method can be used to perform asynchronous execution of a function, in which the input data is data from an array (selection from a MySQL database)? Nothing but cycles comes to mind. But this is very unprofitable in terms of execution time (because you need to send a POST request to a PHP script, and then write the response data to the database), and there are quite a lot of 10000+ records in the database.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
yeti357, 2015-12-10
@D3fault_Player

Use promises or either type async. Suddenly js supports functional programming features. From an array of objects, you can create an array of promises (or functions), and then start parallel asynchronous execution, and get the result after all the promises / functions in the array have been completed. Parallel launch is done using Promise.all() or async.parallel()

// псевдо код
var yourArrayFromDB = [здесь массив исходных данных, например то, что вы до этого из бд запросили];
var promiseArray = yourArrayFromDB.map( 
    (elem) => new Rromise( function (resolve, reject) {
                // функция которая обрабатывает что вам нужно асинхронно
                request.get('your.url.com', fuction( err, res) {
                     if (err) reject(err);
                     resolve(res);
                })
            }
        )
);

Promise.all(promiseArray)
    .then((resArray) => { здесь делаете что хотите с массивом результатов асинхронных функций })
    .catch(console.log);

H
HoHsi, 2015-12-08
@HoHsi

1) AMQP , and delegation of tasks.
2) Run child processes , split the array into chunks and distribute them to coroutines.
First way :
Benefits:
* Unlimited by anything. Run `workers X number of cores` . You buy another car, put the same program on it. Etc.
* Even if the process has crashed, part of the array will not be lost, but will be transferred to another worker.
Disadvantages:
* It takes time to complete the transaction (a far-fetched disadvantage, since the transaction is completed in a couple of milliseconds).
Second way :
Benefits:
* Instant transactions (well, or as the parent process responds).
Disadvantages:
* Limited number of CPUs.
* Do not expand

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question