1 |
|
2 |
|
3 |
|
4 |
|
5 | (function(define) { 'use strict';
|
6 | define(function(require) {
|
7 |
|
8 | var when = require('./when');
|
9 | var slice = Array.prototype.slice;
|
10 | var Promise = when.Promise;
|
11 | var reject = Promise.reject;
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function lift(generator) {
|
20 | return function() {
|
21 | return run(generator, this, arguments);
|
22 | };
|
23 | }
|
24 |
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function call(generator /*x, y, z...*/) {
|
35 |
|
36 | return run(generator, this, slice.call(arguments, 1));
|
37 | }
|
38 |
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function apply(generator, args) {
|
49 |
|
50 | return run(generator, this, args || []);
|
51 | }
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 | function run(generator, thisArg, args) {
|
58 | return runNext(void 0, generator.apply(thisArg, args));
|
59 | }
|
60 |
|
61 | function runNext(x, iterator) {
|
62 | try {
|
63 | return handle(iterator.next(x), iterator);
|
64 | } catch(e) {
|
65 | return reject(e);
|
66 | }
|
67 | }
|
68 |
|
69 | function next(x) {
|
70 |
|
71 | return runNext(x, this);
|
72 | }
|
73 |
|
74 | function error(e) {
|
75 |
|
76 | try {
|
77 | return handle(this.throw(e), this);
|
78 | } catch(e) {
|
79 | return reject(e);
|
80 | }
|
81 | }
|
82 |
|
83 | function handle(result, iterator) {
|
84 | if(result.done) {
|
85 | return result.value;
|
86 | }
|
87 |
|
88 | var h = Promise._handler(result.value);
|
89 | if(h.state() > 0) {
|
90 | return runNext(h.value, iterator);
|
91 | }
|
92 |
|
93 | var p = Promise._defer();
|
94 | h.chain(p._handler, iterator, next, error);
|
95 | return p;
|
96 | }
|
97 |
|
98 | return {
|
99 | lift: lift,
|
100 | call: call,
|
101 | apply: apply
|
102 | };
|
103 |
|
104 | });
|
105 | }(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));
|