mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-05 15:37:03 +03:00
Promises (first look)
Thread dropdown Small fixes
This commit is contained in:
parent
dd41d30f9b
commit
c23ba31e17
61 changed files with 12442 additions and 130 deletions
36
vendors/Q/examples/async-generators/0.html
vendored
Normal file
36
vendors/Q/examples/async-generators/0.html
vendored
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Q.async animation example</title>
|
||||
<!--
|
||||
Works in browsers that support ES6 geneartors, like Chromium 29 with
|
||||
the --harmony flag.
|
||||
|
||||
Peter Hallam, Tom van Cutsem, Mark S. Miller, Dave Herman, Andy Wingo.
|
||||
The animation example was taken from
|
||||
<http://wiki.ecmascript.org/doku.php?id=strawman:deferred_functions>
|
||||
-->
|
||||
</head>
|
||||
<body>
|
||||
<div id="box" style="width: 20px; height: 20px; background-color: red;"></div>
|
||||
|
||||
<script src="../../q.js"></script>
|
||||
<script>
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
var deferredAnimate = Q.async(function* (element) {
|
||||
for (var i = 0; i < 100; ++i) {
|
||||
element.style.marginLeft = i + "px";
|
||||
yield Q.delay(20);
|
||||
}
|
||||
});
|
||||
|
||||
Q.spawn(function* () {
|
||||
yield deferredAnimate(document.getElementById("box"));
|
||||
alert("Done!");
|
||||
});
|
||||
}());
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
19
vendors/Q/examples/async-generators/1-return.js
vendored
Normal file
19
vendors/Q/examples/async-generators/1-return.js
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
"use strict";
|
||||
|
||||
var Q = require("../../q");
|
||||
|
||||
var generator = Q.async(function* () {
|
||||
var ten = yield 10;
|
||||
console.log(ten, 10);
|
||||
var twenty = yield ten + 10;
|
||||
console.log(twenty, 20);
|
||||
var thirty = yield twenty + 10;
|
||||
console.log(thirty, 30);
|
||||
return thirty + 10;
|
||||
});
|
||||
|
||||
generator().then(function (forty) {
|
||||
console.log(forty, 40);
|
||||
}, function (reason) {
|
||||
console.log("reason", reason);
|
||||
});
|
||||
21
vendors/Q/examples/async-generators/2-error-propagation.js
vendored
Normal file
21
vendors/Q/examples/async-generators/2-error-propagation.js
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
"use strict";
|
||||
|
||||
var Q = require("../../q");
|
||||
|
||||
var generator = Q.async(function* () {
|
||||
try {
|
||||
var ten = yield Q.reject(new Error("Rejected!"));
|
||||
console.log("Should not get here 1");
|
||||
} catch (exception) {
|
||||
console.log("Should get here 1");
|
||||
console.log(exception.message, "should be", "Rejected!");
|
||||
throw new Error("Threw!");
|
||||
}
|
||||
});
|
||||
|
||||
generator().then(function () {
|
||||
console.log("Should not get here 2");
|
||||
}, function (reason) {
|
||||
console.log("Should get here 2");
|
||||
console.log(reason.message, "should be", "Threw!");
|
||||
});
|
||||
21
vendors/Q/examples/async-generators/3-spawn.js
vendored
Normal file
21
vendors/Q/examples/async-generators/3-spawn.js
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
"use strict";
|
||||
|
||||
var Q = require("../../q");
|
||||
|
||||
function foo() {
|
||||
return Q.delay(5, 1000);
|
||||
}
|
||||
|
||||
function bar() {
|
||||
return Q.delay(10, 1000);
|
||||
}
|
||||
|
||||
Q.spawn(function* () {
|
||||
var x = yield foo();
|
||||
console.log(x);
|
||||
|
||||
var y = yield bar();
|
||||
console.log(y);
|
||||
|
||||
console.log("result", x + y);
|
||||
});
|
||||
42
vendors/Q/examples/async-generators/4-flow-control.js
vendored
Normal file
42
vendors/Q/examples/async-generators/4-flow-control.js
vendored
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
"use strict";
|
||||
|
||||
var Q = require("../../q");
|
||||
|
||||
// We get back blocking semantics: can use promises with `if`, `while`, `for`,
|
||||
// etc.
|
||||
var filter = Q.async(function* (promises, test) {
|
||||
var results = [];
|
||||
for (var i = 0; i < promises.length; i++) {
|
||||
var val = yield promises[i];
|
||||
if (test(val)) {
|
||||
results.push(val);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
});
|
||||
|
||||
var promises = [
|
||||
Q.delay("a", 500),
|
||||
Q.delay("d", 1000),
|
||||
Q("l")
|
||||
];
|
||||
|
||||
filter(promises, function (letter) {
|
||||
return "f" > letter;
|
||||
}).done(function (all) {
|
||||
console.log(all); // [ "a", "d" ]
|
||||
});
|
||||
|
||||
|
||||
// we can use try and catch to handle rejected promises
|
||||
var logRejections = Q.async(function* (work) {
|
||||
try {
|
||||
yield work;
|
||||
console.log("Never end up here");
|
||||
} catch (e) {
|
||||
console.log("Caught:", e.message);
|
||||
}
|
||||
});
|
||||
|
||||
var rejection = Q.reject(new Error("Oh dear"));
|
||||
logRejections(rejection); // Caught: Oh dear
|
||||
66
vendors/Q/examples/async-generators/README.md
vendored
Normal file
66
vendors/Q/examples/async-generators/README.md
vendored
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
:warning: Warning: The behavior described here is likely to be quickly
|
||||
obseleted by developments in standardization and implementation. Tread with
|
||||
care.
|
||||
|
||||
Q has an `async` function. This can be used to decorate a generator function
|
||||
such that `yield` is effectively equivalent to `await` or `defer` syntax as
|
||||
supported by languages like Go and C# 5.
|
||||
|
||||
Generator functions are presently on the standards track for ES6. As of July
|
||||
2013, they are only fully supported by bleeding edge V8, which hasn't made it
|
||||
out to a released Chromium yet but will probably be in Chromium 29. Even then,
|
||||
they must be enabled from [chrome://flags](chrome://flags) as "Experimental
|
||||
JavaScript features." SpiderMonkey (used in Firefox) includes an older style of
|
||||
generators, but these are not supported by Q.
|
||||
|
||||
Here's an example of using generators by themselves, without any Q features:
|
||||
|
||||
```js
|
||||
function* count() {
|
||||
var i = 0;
|
||||
while (true) {
|
||||
yield i++;
|
||||
}
|
||||
}
|
||||
|
||||
var counter = count();
|
||||
counter.next().value === 0;
|
||||
counter.next().value === 1;
|
||||
counter.next().value === 2;
|
||||
```
|
||||
|
||||
`yield` can also return a value, if the `next` method of the generator is
|
||||
called with a parameter:
|
||||
|
||||
```js
|
||||
var buffer = (function* () {
|
||||
var x;
|
||||
while (true) {
|
||||
x = yield x;
|
||||
}
|
||||
}());
|
||||
|
||||
buffer.next(1).value === undefined;
|
||||
buffer.next("a").value === 1;
|
||||
buffer.value(2).value === "a";
|
||||
buffer.next().value === 2;
|
||||
buffer.next().value === undefined;
|
||||
buffer.next().value === undefined;
|
||||
```
|
||||
|
||||
Inside functions wrapped with `Q.async`, we can use `yield` to wait for a
|
||||
promise to settle:
|
||||
|
||||
```js
|
||||
var eventualAdd = Q.async(function* (oneP, twoP) {
|
||||
var one = yield oneP;
|
||||
var two = yield twoP;
|
||||
return one + two;
|
||||
});
|
||||
|
||||
eventualAdd(eventualOne, eventualTwo).then(function (three) {
|
||||
three === 3;
|
||||
});
|
||||
```
|
||||
You can see more examples of how this works, as well as the `Q.spawn` function,
|
||||
in the other files in this folder.
|
||||
Loading…
Add table
Add a link
Reference in a new issue