Manual using generator with yield
without await
· Issue #117 · tc39/proposal-async-iteration (original) (raw)
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
- I expect that
point#2
andtask#2
will be called immediately(within seconditerator.next()
), because there is noawait
for thetask#1
I give this behavior atbabel-node
, but not inChrome Canary
Chrome executepoint#2
only whentask#1
resolved, like as I wroteyield await task('task#1')
insteadyield task('task#1')
- Should the
promiseCapability/IteratorResult
be resolved only when theresult
is resolved, and contain resolved value?
2.1) Inbabel-node
IteratorResult
resolved, whenyield
got the value(pending Promise), and return this value without additional awaiting.
In other word IteratorResult..value can be aPromise
2.2) InChrome
IteratorResult
resolved, whenyield
got the value(pending Promise), and this value also resolved.
In other word IteratorResult..value cannot be aPromise
- 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"