|
23 | 23 | * It can be used like so:
|
24 | 24 | *
|
25 | 25 | * ```js
|
26 |
| - * return $q(function(resolve, reject) { |
27 |
| - * // perform some asynchronous operation, resolve or reject the promise when appropriate. |
28 |
| - * setInterval(function() { |
29 |
| - * if (pollStatus > 0) { |
30 |
| - * resolve(polledValue); |
31 |
| - * } else if (pollStatus < 0) { |
32 |
| - * reject(polledValue); |
33 |
| - * } else { |
34 |
| - * pollStatus = pollAgain(function(value) { |
35 |
| - * polledValue = value; |
36 |
| - * }); |
37 |
| - * } |
38 |
| - * }, 10000); |
39 |
| - * }). |
40 |
| - * then(function(value) { |
41 |
| - * // handle success |
| 26 | + * // for the purpose of this example let's assume that variables `$q` and `okToGreet` |
| 27 | + * // are available in the current lexical scope (they could have been injected or passed in). |
| 28 | + * |
| 29 | + * function asyncGreet(name) { |
| 30 | + * // perform some asynchronous operation, resolve or reject the promise when appropriate. |
| 31 | + * return $q(function(resolve, reject) { |
| 32 | + * setTimeout(function() { |
| 33 | + * if (okToGreet(name)) { |
| 34 | + * resolve('Hello, ' + name + '!'); |
| 35 | + * } else { |
| 36 | + * reject('Greeting ' + name + ' is not allowed.'); |
| 37 | + * } |
| 38 | + * }, 1000); |
| 39 | + * }); |
| 40 | + * } |
| 41 | + * |
| 42 | + * var promise = asyncGreet('Robin Hood'); |
| 43 | + * promise.then(function(greeting) { |
| 44 | + * alert('Success: ' + greeting); |
42 | 45 | * }, function(reason) {
|
43 |
| - * // handle failure |
| 46 | + * alert('Failed: ' + reason); |
44 | 47 | * });
|
45 | 48 | * ```
|
46 | 49 | *
|
|
56 | 59 | * asynchronous programming what `try`, `catch` and `throw` keywords are to synchronous programming.
|
57 | 60 | *
|
58 | 61 | * ```js
|
59 |
| - * // for the purpose of this example let's assume that variables `$q`, `scope` and `okToGreet` |
| 62 | + * // for the purpose of this example let's assume that variables `$q` and `okToGreet` |
60 | 63 | * // are available in the current lexical scope (they could have been injected or passed in).
|
61 | 64 | *
|
62 | 65 | * function asyncGreet(name) {
|
|
0 commit comments