Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Manual using generator with yield without await #117

@Gvozd

Description

@Gvozd

I try code below in babel-node, and Chrome Canary(v64), and get different result
Unfortunately I could not understand what behavior is correct by this spec

In this example I call iterator.next() twice, and synchronously

  1. I expect that point#2 and task#2 will be called immediately(within second iterator.next()), because there is no await for the task#1
    I give this behavior at babel-node, but not in Chrome Canary
    Chrome execute point#2 only when task#1 resolved, like as I wrote yield await task('task#1') instead yield task('task#1')
  2. Should the promiseCapability/IteratorResult be resolved only when the result is resolved, and contain resolved value?
    2.1) In babel-node IteratorResult resolved, when yield got the value(pending Promise), and return this value without additional awaiting.
    In other word IteratorResult..value can be a Promise
    2.2) In Chrome IteratorResult resolved, when yield got the value(pending Promise), and this value also resolved.
    In other word IteratorResult..value cannot be a Promise - it is always fulfilled value

I read "Async Generator Rewrite", and it turns out that the behavior of the babel is correct
But when I read #114, I'm confused

What is correct behavior for this example?

const start = Date.now(),
    iterator = iteratorBar();
logIteratorResult('next#1', iterator.next());
logIteratorResult('next#2', iterator.next());

async function* iteratorBar() {
    console.log(time(), 'point#1');
    yield task('task#1');
    console.log(time(), 'point#2');
    yield task('task#2');
}

async function task(value) {
    console.log(time(), `${value} - started`);
    return new Promise((resolve) => setTimeout(resolve, 1000, value));
}

async function logIteratorResult(name, iteratorResult) {
    let {value, done} = await iteratorResult;
    console.log(time(), `${name} IteratorResult - resolved`, value);
    value = await value;
    console.log(time(), `${name} IteratorResult.value - resolved`, value);
}

function time() {
    return Date.now() - start;
}

babel-node execution log

1 'point#1'
3 'task#1 - started'
4 'point#2'
4 'task#2 - started'
34 'next#1 IteratorResult - resolved' Promise { <pending> }
34 'next#2 IteratorResult - resolved' Promise { <pending> }
1004 'next#1 IteratorResult.value - resolved' 'task#1'
1004 'next#2 IteratorResult.value - resolved' 'task#2'

Chrome Canary execution log

1 "point#1"
2 "task#1 - started"
1002 "point#2"
1002 "task#2 - started"
1003 "next#1 IteratorResult - resolved" "task#1"
1003 "next#1 IteratorResult.value - resolved" "task#1"
2003 "next#2 IteratorResult - resolved" "task#2"
2004 "next#2 IteratorResult.value - resolved" "task#2"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions