Skip to content

Commit

Permalink
Add support for await with the pipeline operator
Browse files Browse the repository at this point in the history
  • Loading branch information
ksmithbaylor committed Jan 4, 2018
1 parent 8250ff9 commit 5d539fd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/babel-plugin-proposal-pipeline-operator/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function() {

let optimizeArrow =
t.isArrowFunctionExpression(right) && t.isExpression(right.body);
const isAwait = t.isAwaitExpression(right);
let param;

if (optimizeArrow) {
Expand All @@ -24,6 +25,11 @@ export default function() {
} else if (params.length > 0) {
optimizeArrow = false;
}
} else if (isAwait) {
right.argument = t.sequenceExpression([
t.numericLiteral(0),
right.argument,
]);
} else if (t.isIdentifier(right, { name: "eval" })) {
right = t.sequenceExpression([t.numericLiteral(0), right]);
}
Expand All @@ -44,7 +50,9 @@ export default function() {

const call = optimizeArrow
? right.body
: t.callExpression(right, [placeholder]);
: isAwait
? t.awaitExpression(t.callExpression(right.argument, [placeholder]))
: t.callExpression(right, [placeholder]);
path.replaceWith(
t.sequenceExpression([
t.assignmentExpression("=", placeholder, left),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var incPromise = (x) => Promise.resolve(x + 1);
var double = (x) => x * 2;

var result = async () => 10 |> await incPromise;
var result2 = async () => 10 |> await incPromise |> double;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var incPromise = x => Promise.resolve(x + 1);
var double = x => x * 2;

var result = async () => 10 |> await incPromise;
var result2 = async () => 10 |> await incPromise |> double;

return Promise.all([result(), result2()]).then(([r, r2]) => {
assert.equal(r, 11);
assert.equal(r2, 22);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var incPromise = x => Promise.resolve(x + 1);

var double = x => x * 2;

var result = async () => {
var _;

return _ = 10, await (0, incPromise)(_);
};

var result2 = async () => {
var _ref, _2;

return _ref = (_2 = 10, await (0, incPromise)(_2)), double(_ref);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"parserOpts": {
"allowReturnOutsideFunction": true
}
}

0 comments on commit 5d539fd

Please sign in to comment.