This repository was archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
This repository was archived by the owner on Jan 26, 2022. It is now read-only.
Manual using generator with yield without await #117
Copy link
Copy link
Closed
Description
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#2andtask#2will be called immediately(within seconditerator.next()), because there is noawaitfor thetask#1
I give this behavior atbabel-node, but not inChrome Canary
Chrome executepoint#2only whentask#1resolved, like as I wroteyield await task('task#1')insteadyield task('task#1') - Should the
promiseCapability/IteratorResultbe resolved only when theresultis resolved, and contain resolved value?
2.1) Inbabel-nodeIteratorResultresolved, whenyieldgot the value(pending Promise), and return this value without additional awaiting.
In other word IteratorResult..value can be aPromise
2.2) InChromeIteratorResultresolved, whenyieldgot 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"
Metadata
Metadata
Assignees
Labels
No labels