最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ecmascript 6 - JavaScript Generators - How to skip yield when called using .next() ? - Stack Overflow

programmeradmin5浏览0评论

JavaScript generators allow you to yield actions in a procedural manner.

Is it possible to skip/invoke specific yields natively?

Given the below example, how could this be achieved?

I would like to yield values 1, 3 & 5.

function *getVal() {
    yield 1;
    yield 2;
    yield 3;
    yield 4;
    yield 5;
} 


let x = getVal();

// I want to yield ONLY values 1 , 3 , & 5

// Here val will equal 1
let val = x.next();

// I now want to val to equal 3
val = << skip second yield and hit 3 >>

// Is it possible to skip a yield natively?
// ...

JavaScript generators allow you to yield actions in a procedural manner.

Is it possible to skip/invoke specific yields natively?

Given the below example, how could this be achieved?

I would like to yield values 1, 3 & 5.

function *getVal() {
    yield 1;
    yield 2;
    yield 3;
    yield 4;
    yield 5;
} 


let x = getVal();

// I want to yield ONLY values 1 , 3 , & 5

// Here val will equal 1
let val = x.next();

// I now want to val to equal 3
val = << skip second yield and hit 3 >>

// Is it possible to skip a yield natively?
// ...

Share Improve this question edited May 20, 2023 at 16:48 Jonas 129k102 gold badges327 silver badges405 bronze badges asked Jul 12, 2018 at 21:24 Nicholas PorterNicholas Porter 2,9914 gold badges24 silver badges40 bronze badges 6
  • 2 If you want to skip one yield, why not just call x.next() before assigning x.next() to val? – Ivan Commented Jul 12, 2018 at 21:29
  • 1 You can't skip a yield, but you can call it and ignore the result. – Barmar Commented Jul 12, 2018 at 21:32
  • @Ivan Thank you, I'm aware you can just call without assigning, but how about skipping without having to invoke the one that is going to be skipped. – Nicholas Porter Commented Jul 12, 2018 at 21:48
  • @Nicholas, this assumes you know the value that will be yielded by the generator, right? – Ivan Commented Jul 12, 2018 at 21:51
  • 2 I think the thing to keep in mind is that a generator still behaves like a function. A yield pauses the function until it starts again, but you can't skip part of a generator from outside any more than you can reach in and skip part of any other function. – loganfsmyth Commented Jul 13, 2018 at 1:45
 |  Show 1 more ment

2 Answers 2

Reset to default 8

Generators follow the javascript iterator protocol, so there aren't many options to control them beyond calling next().

But, since you are in control of the logic of the generator, you can define the behavior you want for each of those calls to next(). If you want to skip numbers, just make a way to municate that to the generator.

For example, this generator will make consecutive numbers, but skip based on the number passed into next()

function *getVal() {
    let n = 1;
    let skip = 0
    while (n <= 15){
        skip =  yield n
        n = n+1+ (skip || 0)
    }
} 


let x = getVal();

console.log(x.next().value);  // start with 1
console.log(x.next(1).value); // skip two
console.log(x.next().value)
console.log(x.next(2).value)  // skip 5 and 6
console.log(x.next(1).value); // skip 8
//etc.

You can always call the next value without assigning it, then call .next again and assign it:

function *getVal() {
    yield 1;
    yield 2;
    yield 3;
    yield 4;
    yield 5;
} 


let x = getVal();

let val = x.next().value;   // 1
console.log(val);           

x.next();
val = x.next().value;       // 3
console.log(val);

x.next();
val = x.next().value;       // 5
console.log(val);

This way you are ignoring some of the yielded values.

发布评论

评论列表(0)

  1. 暂无评论