forked from kriskowal/q
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update all examples to modern Q usage and style.
Also fixed accuracy of the generators readme.
- Loading branch information
Showing
15 changed files
with
118 additions
and
386 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 was deleted.
Oops, something went wrong.
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,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); | ||
}); |
This file was deleted.
Oops, something went wrong.
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,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!"); | ||
}); |
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 |
---|---|---|
@@ -1,40 +1,42 @@ | ||
var Q = require('../../q'); | ||
"use strict"; | ||
|
||
// we get back blocking semantics - can use promises | ||
// with if, while, for etc | ||
var filter = Q.async(function*(promises,test) { | ||
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++) { | ||
for (var i = 0; i < promises.length; i++) { | ||
var val = yield promises[i]; | ||
if(test(val)) results.push(val); | ||
if (test(val)) { | ||
results.push(val); | ||
} | ||
} | ||
return results; | ||
}); | ||
|
||
var alreadyResolved = Q; | ||
var promises = [ | ||
Q.delay("a",500), | ||
Q.delay("d",1000), | ||
alreadyResolved("l") | ||
Q.delay("a", 500), | ||
Q.delay("d", 1000), | ||
Q("l") | ||
]; | ||
|
||
filter(promises,function(letter) { | ||
filter(promises, function (letter) { | ||
return "f" > letter; | ||
}).done(function(all) { | ||
}).done(function (all) { | ||
console.log(all); // [ "a", "d" ] | ||
}); | ||
|
||
|
||
// we can use try and catch to handle rejected promises | ||
var logRejections = Q.async(function*(work) { | ||
var logRejections = Q.async(function* (work) { | ||
try { | ||
yield work; | ||
console.log("Never end up here"); | ||
} catch(e) { | ||
console.log("Caught:",e); | ||
} catch (e) { | ||
console.log("Caught:", e.message); | ||
} | ||
}); | ||
|
||
var reject = Q.defer(); | ||
reject.reject("Oh dear"); | ||
logRejections(reject.promise); // Caught: Oh dear | ||
var rejection = Q.reject(new Error("Oh dear")); | ||
logRejections(rejection); // Caught: Oh dear |
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
Oops, something went wrong.