Skip to content

Commit 51b8884

Browse files
committed
Added README, LICENSE, Compiled File and Package.json
1 parent 20853e4 commit 51b8884

File tree

5 files changed

+276
-0
lines changed

5 files changed

+276
-0
lines changed

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2016 James "The Jamesernator" Browning
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Async
2+
3+
This is a basic wrapper around generators to create async functions:
4+
5+
```javascript
6+
var async = require('es6-simple-async');
7+
8+
var timer = function(time) {
9+
return new Promise(function(resolve) {
10+
setTimeout(resolve, time);
11+
});
12+
};
13+
14+
var delay = async(function*(time, message) {
15+
yield timer(time);
16+
console.log(message);
17+
});
18+
19+
async.main(function*() {
20+
yield delay(1000, "Hello");
21+
yield delay(2000, "Goodbye");
22+
});
23+
24+
```
25+
26+
## Functions
27+
28+
##### async(genFunction)
29+
This function takes a generator function and returns a function that will return a promise. Inside the generator whenever a yield is reached the value will be turned into a Promise then when the Promise is resolved it will resume the generator sending the value to the generator.
30+
31+
```javascript
32+
var async = require('es6-simple-async');
33+
34+
var timer = function(time) {
35+
return new Promise(function(resolve) {
36+
setTimeout(resolve, time);
37+
});
38+
};
39+
40+
var delay = async(function*(value, time) {
41+
yield timer(1000); // Asynchrously completes the promise then
42+
// resumes the generator at this position
43+
return value; // Resolves the promise of the async function so any
44+
// async function yielding on a Promise returned by this
45+
// function will recieve value when it resumes
46+
});
47+
48+
async.main(function*() {
49+
var value = yield delay('cats', 1000); // recieves value cat after
50+
// 1000 milliseconds
51+
console.log("Value");
52+
});
53+
```
54+
55+
Errors are thrown back into the generator so:
56+
```javascript
57+
58+
var task = async(function*() {
59+
try {
60+
var data = yield request('www.google.com');
61+
} catch (err) {
62+
// If request('www.google.com') rejects for whatever reason we'll
63+
// an error will be thrown at the yield statement so we can just
64+
// wrap it in a try/catch block as usual
65+
console.log("Couldn't get data");
66+
};
67+
});
68+
69+
```
70+
71+
72+
73+
##### async.run(genFunc) / async.do(genFunc)
74+
async.run (or the async.do alias) immediately invokes the given async function and returns a Promise for the value returned:
75+
```javascript
76+
var timer = function(time) {
77+
return new Promise(function(resolve) {
78+
setTimeout(resolve, time);
79+
});
80+
};
81+
82+
async.run(function*() {
83+
yield timer(1000);
84+
return 3;
85+
}).then(function(value) {
86+
console.log(value); // Prints 3 after 1000 seconds
87+
});
88+
89+
```
90+
91+
92+
##### async.main(genFunc)
93+
async.main is much like async.run but if there's an error it will print it to the console.
94+
95+
96+
##### async.from(iterable)
97+
async.from converts an iterable into an async function, this could be useful if creating custom iterators instead of using generators.
98+
99+
```javascript
100+
101+
var async = require('es6-simple-async');
102+
103+
var timer = function(time) {
104+
return new Promise(function(resolve) {
105+
setTimeout(resolve, time);
106+
});
107+
};
108+
109+
var delay = async(function*(value, time) {
110+
yield timer(1000); // Asynchrously completes the promise then
111+
// resumes the generator at this position
112+
return value; // Resolves the promise of the async function so any
113+
// async function yielding on a Promise returned by this
114+
// function will recieve value when it resumes
115+
});
116+
117+
var CustomIterator = function() {
118+
this.value = 0;
119+
};
120+
121+
CustomIterator.prototype.next = function() {
122+
if (this.value < 10) {
123+
this.value = this.value + 1;
124+
var result = delay('cats', 1000);
125+
return {value: result, done: false};
126+
} else {
127+
return {value: 10, done: true};
128+
};
129+
};
130+
131+
CustomIterator.prototype[Symbol.iterator] = function() {
132+
return this;
133+
};
134+
135+
var task = async.from(new CustomIterator());
136+
task().then(function(value) {
137+
console.log(value); // Prints 10 after 10 seconds
138+
});
139+
140+
141+
```

async.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ async.from = (iterable) ->
7373
gen_func = -> yield from iterable
7474
return async(gen_func)
7575

76+
async.do = async.run
77+
7678
if module?
7779
module.exports = async
7880
else

async.js

Lines changed: 105 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "es6-simple-async",
3+
"main": "./async.js",
4+
"repository": {
5+
"type": "git",
6+
"url": "https://github.com/Jamesernator/es6-simple-async"
7+
},
8+
"version": "0.9.9",
9+
"author": "James \"The Jamesernator\" Browning",
10+
"description": "This is a simple async wrapper for generators",
11+
"devDependencies": {
12+
"coffee-script": "latest",
13+
"pre-commit": "latest"
14+
},
15+
"scripts": {
16+
"build": "coffee -o . -c async.coffee"
17+
},
18+
"pre-commit": {
19+
"run": "build"
20+
}
21+
}

0 commit comments

Comments
 (0)