-
Notifications
You must be signed in to change notification settings - Fork 8
/
requirejs-promise.js
69 lines (62 loc) · 2.08 KB
/
requirejs-promise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*! see LICENCE for Simplified BSD Licence */
/*jslint browser:true, indent:2*/
/*global define, require*/ // Require.JS
/*global Promise*/ // ES6 native Promise
define(function () {
'use strict';
var isPromise;
isPromise = function (obj) {
if (!obj || typeof obj !== 'object') {
return false;
}
if (window.Promise && obj instanceof Promise) {
return true;
}
return typeof obj.then === 'function';
};
return {
/**
* @param {String} name This is the name of the desired resource module.
* @param {Function} req Provides a "require" to load other modules.
* @param {Function} load Pass the module's result to this function.
* @param {Object} config Provides the optimizer's configuration.
*/
load: function (name, req, load) { // , config
// TODO: check config.isBuild\
// TODO: call load.fromText() if necessary to eval JavaScript text
req([name], function (result) {
var onReject, onResolve, complete;
onReject = function () {
load.error.apply(null, arguments);
};
onResolve = function () {
load.apply(null, arguments);
};
if (isPromise(result)) {
// If the promise supports "done" (not all do), we want to use that to
// terminate the promise chain and expose any exceptions.
complete = result.done || result.then;
if (typeof result.fail === 'function') {
complete.call(result, onResolve);
result.fail(onReject);
} else {
// native Promises don't have `fail` (thanks @nfeldman)
complete.call(result, onResolve, onReject);
}
} else {
load(result);
}
});
}/*,
write: function () {
// TODO: what needs to be done for write() ??
}, */
/* pluginBuilder: function () {
// TODO: what needs to be done for pluginBuilder() ??
} */
/*
* Note: we explicitly do NOT implement normalize(), as the simpler
* default implementation is sufficient for current use cases.
*/
};
});