Skip to content

Commit b9f8107

Browse files
clmathwkeese
authored andcommitted
Add a plugin to shim the ES6 Promise implementation. fix #19
1 parent 913ee4b commit b9f8107

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

Promise.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Promise plugin.
3+
*
4+
* This plugin returns an ES6 compliant Promise implementation. It returns the implementation from the
5+
* browser if there is one. If the browser does not support Promise, this plugin returns the lie.js
6+
* Promise shim.
7+
*
8+
* @example:
9+
* To create a promise:
10+
* ```
11+
* require(["requirejs-dplugins/Promise!"], function (Promise){
12+
* var promise = new Promise(function (resolve, reject) {
13+
* ...
14+
* });
15+
* });
16+
* ```
17+
*
18+
* @module requirejs-dplugins/Promise
19+
*/
20+
/* global Promise */
21+
define(["require"], function (require) {
22+
return {
23+
load: function (name, req, onload, config) {
24+
config = config || {};
25+
if (config.isBuild) {
26+
onload();
27+
} else if (typeof Promise === "function") {
28+
onload(Promise);
29+
} else {
30+
// use global require to allow map configuration.
31+
require(["lie/dist/lie"], function (lie) {
32+
onload(lie);
33+
});
34+
}
35+
}
36+
};
37+
});

bower.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "AMD plugins for RequireJS",
55
"dependencies": {
66
"jquery": ">=2.1",
7+
"lie": ">=2.8",
78
"requirejs": "2.1.x"
89
},
910
"devDependencies": {},

docs/Promise.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
layout: doc
3+
title: requirejs-dplugins/Promise
4+
---
5+
6+
# requirejs-dplugins/Promise
7+
8+
`requirejs-dplugins/Promise` provides an ES6 Promise implementation. If the browser does not provide it, the
9+
plugin will load the [lie](https://github.com/calvinmetcalf/lie) implementation.
10+
11+
## Sample
12+
```
13+
require(["requirejs-dplugins/Promise!"], function(Promise){
14+
var promise = new Promise(function (resolve, reject) {
15+
...
16+
});
17+
});
18+
```

tests/unit/Promise.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
define([
2+
"intern!object",
3+
"intern/chai!assert"
4+
], function (registerSuite, assert) {
5+
registerSuite({
6+
name: "Promise plugin",
7+
"promise support": function () {
8+
var dfd = this.async(1000);
9+
10+
require(["requirejs-dplugins/Promise!"], function (Promise) {
11+
new Promise(function (resolve) {
12+
setTimeout(resolve, 50);
13+
}).then(function () {
14+
dfd.resolve(true);
15+
});
16+
});
17+
}
18+
});
19+
});

tests/unit/all.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ define([
33
"./css",
44
"./has",
55
"./i18n",
6+
"./Promise",
67
"./jquery"
78
]);

0 commit comments

Comments
 (0)