JavaScript Generators Sending Values to Generator

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


It is possible to send a value to the generator by passing it to the next() method.

function* summer() {
    let sum = 0, value;
    while (true) {
        // receive sent value
        value = yield;
        if (value === null) break;

        // aggregate values
        sum += value;
    return sum;
let generator = summer();

// proceed until the first "yield" expression, ignoring the "value" argument;

// from this point on, the generator aggregates values until we send "null";;;

// close the generator and collect the result
let sum =;  // 111

Got any JavaScript Question?