New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zenparsing async generator functions #4576
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,14 +47,15 @@ function run(task) { | |
|
||
let execCode = exec.code; | ||
let result; | ||
let resultExec; | ||
|
||
if (execCode) { | ||
let execOpts = getOpts(exec); | ||
result = babel.transform(execCode, execOpts); | ||
execCode = result.code; | ||
|
||
try { | ||
runExec(execOpts, execCode); | ||
resultExec = runExec(execOpts, execCode); | ||
} catch (err) { | ||
err.message = exec.loc + ": " + err.message; | ||
err.message += codeFrame(execCode); | ||
|
@@ -90,6 +91,10 @@ function run(task) { | |
chai.expect({ line: expect.line, column: expect.column }).to.deep.equal(actual); | ||
}); | ||
} | ||
|
||
if (execCode && resultExec) { | ||
return resultExec; | ||
} | ||
} | ||
|
||
function runExec(opts, execCode) { | ||
|
@@ -151,7 +156,14 @@ export default function ( | |
return throwMsg === true || err.message.indexOf(throwMsg) >= 0; | ||
}); | ||
} else { | ||
runTask(); | ||
if (task.exec.code) { | ||
let result = run(task); | ||
if (result && typeof result.then === "function") { | ||
return result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically just returning the result of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need the |
||
} | ||
} else { | ||
runTask(); | ||
} | ||
} | ||
}); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
let agf = (() => { | ||
var ref = babelHelpers.asyncGenerator.wrap(function* () { | ||
var _ref = babelHelpers.asyncGenerator.wrap(function* () { | ||
this; | ||
yield babelHelpers.asyncGenerator.await(1); | ||
yield 2; | ||
return 3; | ||
}); | ||
|
||
return function agf() { | ||
return ref.apply(this, arguments); | ||
return _ref.apply(this, arguments); | ||
}; | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
let g = (() => { | ||
var ref = babelHelpers.asyncGenerator.wrap(function* () { | ||
var _ref = babelHelpers.asyncGenerator.wrap(function* () { | ||
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator([1, 2, 3]), babelHelpers.asyncGenerator.await); | ||
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator(iterable), babelHelpers.asyncGenerator.await); | ||
}); | ||
|
||
return function g() { | ||
return ref.apply(this, arguments); | ||
return _ref.apply(this, arguments); | ||
}; | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
let f = (() => { | ||
var ref = babelHelpers.asyncToGenerator(function* () { | ||
var _ref = babelHelpers.asyncToGenerator(function* () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why this happened but some other code changed in the time between the original PR and now |
||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
|
@@ -25,7 +25,8 @@ let f = (() => { | |
} | ||
} | ||
}); | ||
|
||
return function f() { | ||
return ref.apply(this, arguments); | ||
return _ref.apply(this, arguments); | ||
}; | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"use strict"; | ||
|
||
async function* genAnswers() { | ||
var stream = [ Promise.resolve(4), Promise.resolve(9), Promise.resolve(12) ]; | ||
var total = 0; | ||
for await (let val of stream) { | ||
total += await val; | ||
yield total; | ||
} | ||
} | ||
|
||
function forEach(ai, fn) { | ||
return ai.next().then(function (r) { | ||
if (!r.done) { | ||
fn(r); | ||
return forEach(ai, fn); | ||
} | ||
}); | ||
} | ||
|
||
var output = 0; | ||
return forEach(genAnswers(), function(val) { output += val.value }) | ||
.then(function () { | ||
assert.equal(output, 42); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"plugins": [ | ||
"external-helpers", | ||
"transform-async-to-generator", | ||
"transform-async-generator-functions" | ||
], | ||
"presets": ["es2015"], | ||
"parserOpts": { | ||
"allowReturnOutsideFunction": true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"plugins": [ | ||
"external-helpers", | ||
"transform-async-to-generator", | ||
"transform-async-to-generator", | ||
"transform-async-generator-functions" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
let g = (() => { | ||
var ref = babelHelpers.asyncGenerator.wrap(function* (x = babelHelpers.asyncToGenerator(function* () { | ||
var _ref = babelHelpers.asyncGenerator.wrap(function* (x = babelHelpers.asyncToGenerator(function* () { | ||
yield 1; | ||
})) { | ||
yield babelHelpers.asyncGenerator.await(2); | ||
yield 3; | ||
}); | ||
|
||
return function g(_x) { | ||
return ref.apply(this, arguments); | ||
return _ref.apply(this, arguments); | ||
}; | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
let f = (() => { | ||
var ref = babelHelpers.asyncToGenerator(function* () { | ||
var _ref = babelHelpers.asyncToGenerator(function* () { | ||
let g = (() => { | ||
var ref = babelHelpers.asyncGenerator.wrap(function* () { | ||
var _ref2 = babelHelpers.asyncGenerator.wrap(function* () { | ||
yield babelHelpers.asyncGenerator.await(2); | ||
yield 3; | ||
}); | ||
|
||
return function g() { | ||
return ref.apply(this, arguments); | ||
return _ref2.apply(this, arguments); | ||
}; | ||
})(); | ||
|
||
yield 1; | ||
}); | ||
|
||
return function f() { | ||
return ref.apply(this, arguments); | ||
return _ref.apply(this, arguments); | ||
}; | ||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we try to handle the promise case here too, to allow async tests to reject.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should, do you want to do that in another pr if you want to and with an example test?