|
| 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 | +``` |
0 commit comments