ReQL command: each

Command syntax

cursor.each(callback[, onFinishedCallback])
array.each(callback[, onFinishedCallback])
feed.each(callback)

Description

Lazily iterate over the result set one element at a time. The second callback is optional and is called when the iteration stops (when there are no more rows or when the callback returns false).

Example: Let’s process all the elements!

cursor.each(function(err, row) {
    if (err) throw err;
    processRow(row);
});

Example: If we need to know when the iteration is complete, each also accepts a second onFinished callback.

cursor.each(function(err, row) {
        if (err) throw err;
        processRow(row);
    }, function() {
        doneProcessing();
    }
);

Example: Iteration can be stopped prematurely by returning false from the callback. For instance, if you want to stop the iteration as soon as row is negative:

cursor.each(function(err, row) {
    if (err) throw err;

    if (row < 0) {
        cursor.close()
        return false;
    }
    else {
        processRow(row)
    }
});

Note: You need to manually close the cursor if you prematurely stop the iteration.

© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/api/javascript/each/