Permalink
Browse files

* Improve example to feature multiple $await at once.

  Q.all returns an Array object (unlike jQuery.when and Q.spread),
  and so works very well with Jscex's single-value $await.
  • Loading branch information...
1 parent 57fb029 commit 6e0e78f52147ed83057da8ea611caa88d96fa702 @audreyt committed Jun 16, 2012
Showing with 20 additions and 10 deletions.
  1. +2 −2 README.md
  2. +8 −5 example.js
  3. +10 −3 src/example.ls
View
4 README.md
@@ -38,8 +38,8 @@ that returns a Q Promise object.
Functions defined with `Q.async.$` has access to an extra `$await`
keyword, which implicitly waits for other Q Promise objects.
-Please see `example.js` for a sample usage, and type `make demo`
-to see it in action.
+Please see `example.js` for awaiting multiple promises with `Q.all`,
+and type `make demo` to see it in action.
The source code is in `src` directory and written in LiveScript.
Note that LiveScript is _not_ a runtime dependency of this module;
View
13 example.js
@@ -1,19 +1,22 @@
(function(){
- var Q, HTTP, say, fetch;
+ var Q, HTTP, say, $async, fetch;
Q = require('./q-jscex');
HTTP = require('q-http');
say = function(it){
console.log(it);
console.log('');
};
+ $async = Q.async.$;
fetch = eval(Q.async.$(function(url){
- var html;
- html = $await(HTTP.read(url));
- return html.toString('utf-8', 555, 600);
+ var bufs;
+ bufs = $await(Q.all([HTTP.read(url), HTTP.read(url + "/2012/06/jscex-unit-tests-with-mocha-chai.html")]));
+ return bufs.map(function(it){
+ return it.toString('utf-8').replace(/[\d\D]*<title>|<\/title>[\d\D]*$/g, '').trim();
+ });
}));
say('[Demo: Fetching invalid host, expecting ENOENT...]');
fetch('http://blog.zhaojie.error').fail(say).fin(function(){
- say('[Demo: Fetching valid host, expecting snippet...]');
+ say('[Demo: Fetching valid host, expecting snippets...]');
return fetch('http://blog.zhaojie.me').then(say);
});
}).call(this);
View
13 src/example.ls
@@ -1,15 +1,22 @@
Q = require './q-jscex'
HTTP = require \q-http
say = !-> console.log it; console.log ''
+$async = Q.async.$
fetch = eval Q.async.$ (url) ->
- html = $await HTTP.read url
- return html.toString(\utf-8, 555, 600)
+ bufs = $await Q.all [
+ HTTP.read url
+ HTTP.read "#url/2012/06/jscex-unit-tests-with-mocha-chai.html"
+ ]
+ bufs.map ->
+ it.toString \utf-8
+ .replace /[\d\D]*<title>|<\/title>[\d\D]*$/g, ''
+ .trim!
say '[Demo: Fetching invalid host, expecting ENOENT...]'
<- fetch \http://blog.zhaojie.error .fail say .fin
-say '[Demo: Fetching valid host, expecting snippet...]'
+say '[Demo: Fetching valid host, expecting snippets...]'
fetch \http://blog.zhaojie.me .then say

0 comments on commit 6e0e78f

Please sign in to comment.