1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | (function (define) {
|
9 | 'use strict';
|
10 |
|
11 | define(function (require) {
|
12 |
|
13 | var when;
|
14 |
|
15 | when = require('when');
|
16 |
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function lazyPromise(work) {
|
28 | var defer, started, resolver, promise, then;
|
29 |
|
30 | defer = when.defer();
|
31 | started = false;
|
32 |
|
33 | resolver = defer.resolver;
|
34 | promise = defer.promise;
|
35 | then = promise.then;
|
36 |
|
37 | promise.then = function () {
|
38 | if (!started) {
|
39 | started = true;
|
40 | when.attempt(work).then(resolver.resolve, resolver.reject);
|
41 | }
|
42 | return then.apply(promise, arguments);
|
43 | };
|
44 |
|
45 | return promise;
|
46 | }
|
47 |
|
48 | return lazyPromise;
|
49 |
|
50 | });
|
51 |
|
52 | }(
|
53 | typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
|
54 |
|
55 | ));
|