forked from kriskowal/q
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Q.async to work with current ES6 generators
ES6 generators are slightly different from what has been shipped in SpiderMonkey. For our purposes, the differences are: * they use function* as a token; * they box their return value in a { value: <any>, done: <bool> } object; and * they can return a value. They are close enough to SpiderMonkey that Q.async can support both. This commit checks for legacy generators at Q startup, and expects unboxed values in that case. This commit also updates the examples. In particular, the wishful thinking and shim examples go away, as they are handled naturally with the new specification. A copy of the legacy examples has been moved to the examples/async-generators/js-1.7/ folder.
- Loading branch information
Showing
10 changed files
with
239 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<!-- | ||
Works in Firefox, or where generators are implemented. | ||
Peter Hallam, Tom van Cutsem, Mark S. Miller, Dave Herman. | ||
The animation example was taken from | ||
<http://wiki.ecmascript.org/doku.php?id=strawman:deferred_functions> | ||
--> | ||
<script src="../../../q.js"></script> | ||
<script type="application/javascript;version=1.7"> | ||
|
||
function delay(millis, answer) { | ||
const deferredResult = Q.defer(); | ||
setTimeout(function() { | ||
deferredResult.resolve(answer); | ||
}, millis); | ||
return deferredResult.promise; | ||
} | ||
|
||
var deferredAnimate = Q.async(function(element) { | ||
for (var i = 0; i < 100; ++i) { | ||
element.style.marginLeft = ''+i+'px'; | ||
yield delay(20); | ||
} | ||
}); | ||
|
||
function test() { | ||
Q.when( | ||
deferredAnimate(document.getElementById('box')), | ||
function () { | ||
alert('Done!'); | ||
} | ||
) | ||
} | ||
|
||
</script> | ||
</head> | ||
<body onload="test()"> | ||
<div id="box" style="width: 20px; height: 20px; background-color: red;"></div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<!-- | ||
Works in Firefox, or where generators are implemented. | ||
--> | ||
<script src="../../../q.js"></script> | ||
<script type="application/javascript;version=1.7"> | ||
|
||
function test() { | ||
|
||
var generator = Q.async(function () { | ||
try { | ||
var ten = yield Q.reject('Rejected!'); | ||
console.log("Should not get here 1"); | ||
} catch (exception) { | ||
console.log("Should get here 1"); | ||
console.log(exception, "should be", "Rejected!"); | ||
throw "Threw!"; | ||
} | ||
}); | ||
|
||
Q.when(generator(), function () { | ||
console.log("Should not get here 2"); | ||
}, function (reason) { | ||
console.log("Should get here 2"); | ||
console.log(reason, "should be", "Threw!"); | ||
}); | ||
|
||
} | ||
|
||
</script> | ||
</head> | ||
<body onload="test()"> | ||
<div id="box" style="width: 20px; height: 20px; background-color: red;"></div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
/!\ Warning: The behavior described here corresponds to a dead-end fork | ||
of JavaScript used in FireFox. If you can, use ES6-style generators, | ||
described [in the parent directory](../). Currently Q works with both | ||
kinds of generators, but perhaps in a year or so, support for the older | ||
SpiderMonkey generators will go away. | ||
|
||
Examples of SpiderMonkey-style generators: | ||
|
||
function count() { | ||
var i = 0; | ||
while (true) { | ||
yield i++; | ||
} | ||
} | ||
|
||
var counter = count(); | ||
count.next() === 0; | ||
count.next() === 1; | ||
count.next() === 2; | ||
|
||
In this case it's just like ES6 generators, but with ``function`` | ||
instead of ``function*``. Like ES6 generators, ``yield`` can also | ||
return a value, if the ``send`` method of a generator is used instead of | ||
``next``: | ||
|
||
var buffer = (function () { | ||
var x; | ||
while (true) { | ||
x = yield x; | ||
} | ||
}()); | ||
|
||
buffer.send(1) === undefined | ||
buffer.send("a") === 1 | ||
buffer.send(2) === "a" | ||
buffer.next() === 2 | ||
buffer.next() === undefined | ||
buffer.next() === undefined | ||
|
||
We can use ``yield`` to wait for a promise to resolve. | ||
|
||
var eventualAdd = Q.async(function (oneP, twoP) { | ||
var one = yield oneP; | ||
var two = yield twoP; | ||
Q.return(one + two); | ||
}); | ||
|
||
eventualAdd(eventualOne, eventualTwo) | ||
.then(function (three) { | ||
three === 3; | ||
}); | ||
|
||
Note! SpiderMonkey does not allow return values in generators. To work | ||
around that, call the ``Q.return`` function, as used above, instead of | ||
using a ``return`` statement. ``Q.return`` will go away at some point | ||
when SpiderMonkey switches to ES6 style. |
Oops, something went wrong.