├── .gitignore ├── .idea ├── .name ├── codeStyleSettings.xml ├── compiler.xml ├── encodings.xml ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── jsLibraryMappings.xml ├── jsLinters │ └── jslint.xml ├── libraries │ ├── Node_js_Dependencies_for_requirejs_promise.xml │ └── bower_components.xml ├── misc.xml ├── modules.xml ├── scopes │ └── scope_settings.xml └── vcs.xml ├── .travis.yml ├── Gruntfile.js ├── LICENCE ├── README.md ├── bower.json ├── examples ├── combination-amd.js ├── combination-commonjs.js ├── es6.js ├── jquery-amd.js ├── jquery-commonjs.js ├── q-amd.js ├── q-commonjs.js ├── require-config.js ├── rsvp-amd.js └── rsvp-commonjs.js ├── package.json ├── requirejs-promise.iml ├── requirejs-promise.js ├── saucelabs.js └── test ├── minified.html ├── tests.js └── unminified.html /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | # output files 4 | test/tests.min.js 5 | 6 | # temporary dependencies 7 | bower_components 8 | node_modules 9 | 10 | # IDE files 11 | .project 12 | /.idea/workspace.xml 13 | /.idea/dictionaries 14 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | requirejs-promise -------------------------------------------------------------------------------- /.idea/codeStyleSettings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 31 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/jsLibraryMappings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/jsLinters/jslint.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/libraries/Node_js_Dependencies_for_requirejs_promise.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/libraries/bower_components.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | 5 | before_script: 6 | - git submodule update --init --recursive 7 | - npm install -g grunt-cli 8 | - npm install -g bower 9 | - bower install 10 | 11 | script: grunt travis 12 | 13 | notifications: 14 | email: false 15 | 16 | env: 17 | global: 18 | - secure: iLTefnFaLgpDZe0HIiukcjXWsYvA1HnofmWw1q4B+Lq4Savlpk8mODt0Wnd85njPiF7CrjDcoxUUQwVtFwDX/9FgkFLkfU5izXtwanO4bWWg9KMqqyUYKpV86KYNEOn5nE3bBNh/dMX3UB/R2AoqF4F8O9wDVBb9AaPCUYIflgc= 19 | - secure: Z3lWrFbTvdVt6nz64TZPVpbvkqlFF283wOQ6jd+hwOUB9e9a9YNULwkRTWRNBl8ztMuUtLrvD0pIdpU5wprDBgeigvNesa+qDcKxNq9xIwidNFg0MaqOrLs98GzEJ1hBkR2LFFovnuJrtEokwGH7Q11/BjFxLpVhxsUbChBnyTI= 20 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | /*jslint indent:2, maxlen:80, node:true*/ 2 | 'use strict'; 3 | 4 | // exports 5 | 6 | module.exports = function (grunt) { 7 | 8 | // Project configuration. 9 | grunt.initConfig({ 10 | 11 | connect: { 12 | server: { 13 | options: { 14 | base: "", 15 | port: 9999 16 | } 17 | } 18 | }, 19 | watch: {}, 20 | 21 | jslint: { 22 | all: { 23 | src: [ 24 | '**/*.js', 25 | '**/*.json', 26 | '!**/*.min.js', 27 | '!node_modules/**/*', 28 | '!bower_components/**/*' 29 | ], 30 | exclude: [], 31 | directives: { 32 | todo: true // TODO: eventually eliminate this exemption 33 | }, 34 | options: { 35 | errorsOnly: true, 36 | failOnError: true 37 | } 38 | } 39 | }, 40 | 41 | mocha: { 42 | all: { 43 | options: { 44 | urls: [ 45 | 'http://127.0.0.1:9999/test/unminified.html', 46 | 'http://127.0.0.1:9999/test/minified.html' 47 | ], 48 | run: true 49 | } 50 | } 51 | }, 52 | 53 | requirejs: { 54 | compile: { 55 | options: { 56 | baseUrl: '.', 57 | mainConfigFile: 'examples/require-config.js', 58 | paths: { 59 | jquery: 'empty:', 60 | q: 'empty:', 61 | rsvp: 'empty:' 62 | }, 63 | name: 'test/tests', 64 | out: 'test/tests.min.js' 65 | } 66 | } 67 | }, 68 | 69 | 'saucelabs-mocha': { 70 | all: { options: require('./saucelabs') } 71 | } 72 | 73 | }); 74 | 75 | // These plugins provide necessary tasks. 76 | grunt.loadNpmTasks('grunt-contrib-connect'); 77 | grunt.loadNpmTasks('grunt-contrib-requirejs'); 78 | grunt.loadNpmTasks('grunt-contrib-watch'); 79 | grunt.loadNpmTasks('grunt-jslint'); 80 | grunt.loadNpmTasks('grunt-mocha'); 81 | grunt.loadNpmTasks('grunt-saucelabs'); 82 | 83 | grunt.registerTask('build', ['requirejs']); 84 | grunt.registerTask('test', ['connect', 'build', 'jslint', 'mocha']); 85 | 86 | grunt.registerTask('travis', ['test', 'saucelabs-mocha']); 87 | grunt.registerTask("dev", ["connect", "watch"]); 88 | 89 | // Default task. 90 | grunt.registerTask('default', ['build', 'test']); 91 | 92 | }; 93 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, Ron Waldon 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | Redistributions in binary form must reproduce the above copyright notice, this 10 | list of conditions and the following disclaimer in the documentation and/or 11 | other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 17 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 20 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # requirejs-promise 2 | 3 | [![Build Status](https://travis-ci.org/jokeyrhyme/requirejs-promise.png?branch=master)](https://travis-ci.org/jokeyrhyme/requirejs-promise) 4 | 5 | This is a plugin for Require.JS that allows modules to resolve asynchronously via Promises. The current AMD standard does not specify how to deal with asynchronous dependencies, and as such it is perfectly reasonable that Require.JS does not handle them. That said, I still find them unavoidable from time to time, so here we are. 6 | 7 | ## Status 8 | 9 | This plugin seems to behave as expected for Promise objects generated by jQuery, Q, and RSVP, as well as native ES6 Promises. 10 | 11 | ## Licence 12 | 13 | I'm putting this out under the Simplified BSD Licence (see LICENCE). Go nuts. 14 | 15 | ## Requirements 16 | 17 | You'll need Require.JS. I aim to always target the current version of Require.JS. 18 | 19 | 20 | ## Usage 21 | 22 | ### Basics 23 | 24 | You shouldn't need to include this directly in your HTML as a script element. 25 | 26 | Configure Require.JS so that it has a path to resolve, for example: 27 | 28 | ```javascript 29 | require.config({ 30 | config: { 31 | paths: { 32 | // remember to leave out the .js on the end 33 | 'promise': '/path/to/requirejs-promise' 34 | } 35 | } 36 | }); 37 | ``` 38 | 39 | Your asynchronous module will probably look something like this: 40 | 41 | ```javascript 42 | define('myAsyncModule', ['jquery'], function($) { 43 | var deferred = new $.Deferred(), 44 | myModule = {}; 45 | 46 | // we'll use AJAX here, but you could do anything asynchronous 47 | $.ajax({ 48 | url: '//domain/path/to/script', 49 | complete: function(jqxhr, textstatus) { 50 | if ($.inArray(jqxhr.status, [0, 200, 204, 304])) { 51 | // TODO: do other stuff for successful AJAX 52 | myModule.response = jqxhr.responseText; 53 | // if your consumers are expecting something, give it to them 54 | deferred.resolve(myModule); 55 | } else { 56 | // TODO: do other stuff for failed AJAX 57 | deferred.reject(null); 58 | } 59 | } 60 | }); 61 | 62 | return deferred.promise(); 63 | }); 64 | ``` 65 | 66 | Then, when it comes time to actually load in an asynchronous Promise-based dependency, do something like this: 67 | 68 | ```javascript 69 | require(['promise!myAsyncModule'], function(asyncModule) { 70 | // TODO: do something cool with asyncModule.response 71 | ... 72 | }); 73 | ``` 74 | 75 | The goal of this little plugin is to make module usage completely seamless. Module resolution is asynchronous within Require.JS anyway, so (assuming everything goes according to plan) nobody downstream should notice that the dependency had a Promise. 76 | 77 | ### Require.JS Optimisation 78 | 79 | See the test/ directory. 80 | 81 | - unminified.html is a QUnit test case using non-optimised sources 82 | 83 | - minified.html is a QUnit test case using optimised (minified) sources 84 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "requirejs-promise", 3 | "description": "plugin for Require.JS that allows modules to resolve asynchronously via Promises", 4 | "version": "1.2.0", 5 | "homepage": "https://github.com/jokeyrhyme/requirejs-promise", 6 | "author": { 7 | "name": "Ron Waldon", 8 | "email": "jokeyrhyme@gmail.com", 9 | "url": "http://jokeyrhy.me/" 10 | }, 11 | "contributors": [], 12 | "repository": { 13 | "type": "git", 14 | "url": "git://github.com/jokeyrhyme/requirejs-promise.git" 15 | }, 16 | "bugs": { 17 | "url": "https://github.com/jokeyrhyme/requirejs-promise/issues" 18 | }, 19 | "licenses": [ 20 | { 21 | "type": "BSD", 22 | "url": "https://github.com/jokeyrhyme/requirejs-promise/blob/master/LICENSE" 23 | } 24 | ], 25 | "main": "requirejs-promise.js", 26 | "dependencies": {}, 27 | "devDependencies": { 28 | "chai": "~1.9", 29 | "jquery": "~2.1", 30 | "mocha": "~1.18", 31 | "q": "~1.0", 32 | "requirejs": "~2.1", 33 | "rsvp": "~3.0" 34 | }, 35 | "peerDependencies": {}, 36 | "keywords": ["requirejs", "deferred", "promise"] 37 | } 38 | -------------------------------------------------------------------------------- /examples/combination-amd.js: -------------------------------------------------------------------------------- 1 | /*jslint browser:true, indent:2, maxlen:80*/ 2 | /*global define, require*/ // Require.JS 3 | 4 | define([ 5 | 'q', 6 | 'promise!examples/jquery-amd', 7 | 'promise!examples/q-commonjs', 8 | 'promise!examples/rsvp-amd' 9 | ], function (Q, ja, qc, ra) { 10 | 'use strict'; 11 | 12 | var dfrd; 13 | 14 | dfrd = Q.defer(); 15 | 16 | setTimeout(function () { 17 | 18 | require([ 19 | 'promise!examples/jquery-commonjs', 20 | 'promise!examples/q-amd', 21 | 'promise!examples/rsvp-commonjs' 22 | ], function (jc, qa, rc) { 23 | dfrd.resolve([ja, jc, qa, qc, ra, rc]); 24 | }); 25 | 26 | }, 0); 27 | 28 | return dfrd.promise; 29 | }); 30 | -------------------------------------------------------------------------------- /examples/combination-commonjs.js: -------------------------------------------------------------------------------- 1 | /*jslint browser:true, indent:2, maxlen:80*/ 2 | /*global define, require*/ // Require.JS 3 | 4 | define(function (require) { 5 | 'use strict'; 6 | 7 | var Q, ja, qc, ra, dfrd; 8 | 9 | Q = require('q'); 10 | ja = require('promise!examples/jquery-amd'); 11 | qc = require('promise!examples/q-commonjs'); 12 | ra = require('promise!examples/rsvp-amd'); 13 | 14 | dfrd = Q.defer(); 15 | 16 | setTimeout(function () { 17 | 18 | require([ 19 | 'promise!examples/jquery-commonjs', 20 | 'promise!examples/q-amd', 21 | 'promise!examples/rsvp-commonjs' 22 | ], function (jc, qa, rc) { 23 | dfrd.resolve([ja, jc, qa, qc, ra, rc]); 24 | }); 25 | 26 | }, 0); 27 | 28 | return dfrd.promise; 29 | }); 30 | -------------------------------------------------------------------------------- /examples/es6.js: -------------------------------------------------------------------------------- 1 | /*jslint browser:true, indent:2, maxlen:80*/ 2 | /*global define, require*/ // Require.JS 3 | 4 | /*global Promise*/ // ES6 native Promise 5 | 6 | define(function () { 7 | 'use strict'; 8 | 9 | var promise; 10 | promise = new Promise(function (resolve) { // , reject 11 | 12 | setTimeout(function () { 13 | resolve('es6'); 14 | }, 0); 15 | 16 | }); 17 | 18 | return promise; 19 | }); 20 | -------------------------------------------------------------------------------- /examples/jquery-amd.js: -------------------------------------------------------------------------------- 1 | /*jslint browser:true, indent:2, maxlen:80*/ 2 | /*global define, require*/ // Require.JS 3 | 4 | define(['jquery'], function ($) { 5 | 'use strict'; 6 | 7 | var dfrd; 8 | dfrd = new $.Deferred(); 9 | 10 | setTimeout(function () { 11 | dfrd.resolve('jquery-amd'); 12 | }, 0); 13 | 14 | return dfrd.promise(); 15 | }); 16 | -------------------------------------------------------------------------------- /examples/jquery-commonjs.js: -------------------------------------------------------------------------------- 1 | /*jslint browser:true, indent:2, maxlen:80*/ 2 | /*global define, require*/ // Require.JS 3 | 4 | define(function (require) { 5 | 'use strict'; 6 | 7 | var $, dfrd; 8 | $ = require('jquery'); 9 | dfrd = new $.Deferred(); 10 | 11 | setTimeout(function () { 12 | dfrd.resolve('jquery-commonjs'); 13 | }, 0); 14 | 15 | return dfrd.promise(); 16 | }); 17 | -------------------------------------------------------------------------------- /examples/q-amd.js: -------------------------------------------------------------------------------- 1 | /*jslint browser:true, indent:2, maxlen:80*/ 2 | /*global define, require*/ // Require.JS 3 | 4 | define(['q'], function (Q) { 5 | 'use strict'; 6 | 7 | var dfrd; 8 | dfrd = Q.defer(); 9 | 10 | setTimeout(function () { 11 | dfrd.resolve('q-amd'); 12 | }, 0); 13 | 14 | return dfrd.promise; 15 | }); 16 | -------------------------------------------------------------------------------- /examples/q-commonjs.js: -------------------------------------------------------------------------------- 1 | /*jslint browser:true, indent:2, maxlen:80*/ 2 | /*global define, require*/ // Require.JS 3 | 4 | define(function (require) { 5 | 'use strict'; 6 | 7 | var Q, dfrd; 8 | Q = require('q'); 9 | dfrd = Q.defer(); 10 | 11 | setTimeout(function () { 12 | dfrd.resolve('q-commonjs'); 13 | }, 0); 14 | 15 | return dfrd.promise; 16 | }); 17 | -------------------------------------------------------------------------------- /examples/require-config.js: -------------------------------------------------------------------------------- 1 | /*jslint browser:true, indent:2, maxlen:80*/ 2 | /*global define, require*/ // Require.JS 3 | 4 | require.config({ 5 | baseUrl: '..', 6 | paths: { 7 | jquery: 'bower_components/jquery/dist/jquery', 8 | q: 'bower_components/q/q', 9 | rsvp: 'bower_components/rsvp/rsvp.amd', 10 | 11 | promise: 'requirejs-promise' 12 | } 13 | }); 14 | -------------------------------------------------------------------------------- /examples/rsvp-amd.js: -------------------------------------------------------------------------------- 1 | /*jslint browser:true, indent:2, maxlen:80*/ 2 | /*global define, require*/ // Require.JS 3 | 4 | define(['rsvp'], function (RSVP) { 5 | 'use strict'; 6 | 7 | var promise; 8 | promise = new RSVP.Promise(function (resolve) { // , reject 9 | 10 | setTimeout(function () { 11 | resolve('rsvp-amd'); 12 | }, 0); 13 | 14 | }); 15 | 16 | return promise; 17 | }); 18 | -------------------------------------------------------------------------------- /examples/rsvp-commonjs.js: -------------------------------------------------------------------------------- 1 | /*jslint browser:true, indent:2, maxlen:80*/ 2 | /*global define, require*/ // Require.JS 3 | 4 | define(function (require) { 5 | 'use strict'; 6 | 7 | var RSVP, promise; 8 | RSVP = require('rsvp'); 9 | promise = new RSVP.Promise(function (resolve) { // , reject 10 | 11 | setTimeout(function () { 12 | resolve('rsvp-commonjs'); 13 | }, 0); 14 | 15 | }); 16 | 17 | return promise; 18 | }); 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "requirejs-promise", 3 | "description": "plugin for Require.JS that allows modules to resolve asynchronously via Promises", 4 | "version": "1.2.0", 5 | "homepage": "https://github.com/jokeyrhyme/requirejs-promise", 6 | "author": { 7 | "name": "Ron Waldon", 8 | "email": "jokeyrhyme@gmail.com", 9 | "url": "http://jokeyrhy.me/" 10 | }, 11 | "contributors": [], 12 | "repository": { 13 | "type": "git", 14 | "url": "git://github.com/jokeyrhyme/requirejs-promise.git" 15 | }, 16 | "bugs": { 17 | "url": "https://github.com/jokeyrhyme/requirejs-promise/issues" 18 | }, 19 | "licenses": [ 20 | { 21 | "type": "BSD", 22 | "url": "https://github.com/jokeyrhyme/requirejs-promise/blob/master/LICENSE" 23 | } 24 | ], 25 | "main": "requirejs-promise.js", 26 | "scripts": {}, 27 | "engines": { 28 | "node": ">=0.9.0 <0.11.0" 29 | }, 30 | "dependencies": {}, 31 | "devDependencies": { 32 | "grunt": "~0.4", 33 | "grunt-contrib-connect": "~0.7", 34 | "grunt-contrib-requirejs": "~0.4", 35 | "grunt-contrib-watch": "~0.6", 36 | "grunt-jslint": "~1.1", 37 | "grunt-mocha": "~0.4", 38 | "grunt-saucelabs": "~5.1" 39 | }, 40 | "peerDependencies": {}, 41 | "keywords": [ 42 | "requirejs", 43 | "deferred", 44 | "promise" 45 | ] 46 | } 47 | -------------------------------------------------------------------------------- /requirejs-promise.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /requirejs-promise.js: -------------------------------------------------------------------------------- 1 | /*! see LICENCE for Simplified BSD Licence */ 2 | /*jslint browser:true, indent:2*/ 3 | /*global define, require*/ // Require.JS 4 | 5 | /*global Promise*/ // ES6 native Promise 6 | 7 | define(function () { 8 | 'use strict'; 9 | 10 | var isPromise; 11 | 12 | isPromise = function (obj) { 13 | if (!obj || typeof obj !== 'object') { 14 | return false; 15 | } 16 | if (window.Promise && obj instanceof Promise) { 17 | return true; 18 | } 19 | return typeof obj.then === 'function'; 20 | }; 21 | 22 | return { 23 | /** 24 | * @param {String} name This is the name of the desired resource module. 25 | * @param {Function} req Provides a "require" to load other modules. 26 | * @param {Function} load Pass the module's result to this function. 27 | * @param {Object} config Provides the optimizer's configuration. 28 | */ 29 | load: function (name, req, load) { // , config 30 | // TODO: check config.isBuild\ 31 | // TODO: call load.fromText() if necessary to eval JavaScript text 32 | req([name], function (result) { 33 | var onReject, onResolve, complete; 34 | onReject = function () { 35 | load.error.apply(null, arguments); 36 | }; 37 | onResolve = function () { 38 | load.apply(null, arguments); 39 | }; 40 | if (isPromise(result)) { 41 | // If the promise supports "done" (not all do), we want to use that to 42 | // terminate the promise chain and expose any exceptions. 43 | complete = result.done || result.then; 44 | 45 | if (typeof result.fail === 'function') { 46 | complete.call(result, onResolve); 47 | result.fail(onReject); 48 | } else { 49 | // native Promises don't have `fail` (thanks @nfeldman) 50 | complete.call(result, onResolve, onReject); 51 | } 52 | 53 | } else { 54 | load(result); 55 | } 56 | }); 57 | }/*, 58 | write: function () { 59 | // TODO: what needs to be done for write() ?? 60 | }, */ 61 | /* pluginBuilder: function () { 62 | // TODO: what needs to be done for pluginBuilder() ?? 63 | } */ 64 | /* 65 | * Note: we explicitly do NOT implement normalize(), as the simpler 66 | * default implementation is sufficient for current use cases. 67 | */ 68 | }; 69 | }); 70 | -------------------------------------------------------------------------------- /saucelabs.js: -------------------------------------------------------------------------------- 1 | /*jslint indent:2, maxlen:80, node:true*/ 2 | 'use strict'; 3 | 4 | // exports 5 | 6 | module.exports = { 7 | urls: [ 8 | 'http://127.0.0.1:9999/test/unminified.html', 9 | 'http://127.0.0.1:9999/test/minified.html' 10 | ], 11 | tunnelTimeout: 5, 12 | build: process.env.TRAVIS_JOB_ID, 13 | concurrency: 3, 14 | browsers: [ 15 | { 16 | browserName: "internet explorer", 17 | version: "11", 18 | platform: "Windows 8.1" 19 | }, 20 | { 21 | browserName: "internet explorer", 22 | version: "10", 23 | platform: "Windows 8" 24 | }, 25 | { 26 | browserName: "firefox", 27 | version: "26", 28 | platform: "Windows 7" 29 | }, 30 | { 31 | browserName: "googlechrome", 32 | version: "31", 33 | platform: "OS X 10.9" 34 | }, 35 | { 36 | browserName: "android", 37 | version: "4.0", 38 | platform: "Linux", 39 | "device-orientation": "portrait" 40 | }, 41 | { 42 | browserName: "iphone", 43 | version: "7", 44 | platform: "OS X 10.9", 45 | "device-orientation": "portrait" 46 | }, 47 | { 48 | browserName: "iphone", 49 | version: "6.1", 50 | platform: "OS X 10.8", 51 | "device-orientation": "portrait" 52 | } 53 | ], 54 | testname: 'requirejs-promise', 55 | tags: ['master'] 56 | }; 57 | -------------------------------------------------------------------------------- /test/minified.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /test/tests.js: -------------------------------------------------------------------------------- 1 | /*jslint browser:true, indent:2, maxlen:80*/ 2 | /*global define, require*/ // Require.JS 3 | 4 | /*global suite, test, setup, teardown, suiteSetup, suiteTeardown*/ // mocha 5 | /*global assert*/ // chai 6 | 7 | /*global Promise*/ // ES6 native Promise 8 | 9 | suite('jQuery Promise example', function () { 10 | 'use strict'; 11 | 12 | test('AMD: direct require', function (done) { 13 | require(['examples/jquery-amd'], function (promise) { 14 | assert(promise); 15 | assert.isObject(promise); 16 | assert.isFunction(promise.then); 17 | done(); 18 | }); 19 | }); 20 | 21 | test('AMD: require with promise!', function (done) { 22 | require(['promise!examples/jquery-amd'], function (result) { 23 | assert(result); 24 | assert.isString(result); 25 | assert.equal(result, 'jquery-amd'); 26 | done(); 27 | }); 28 | }); 29 | 30 | test('CommonJS: direct require', function (done) { 31 | require(['examples/jquery-commonjs'], function (promise) { 32 | assert(promise); 33 | assert.isObject(promise); 34 | assert.isFunction(promise.then); 35 | done(); 36 | }); 37 | }); 38 | 39 | test('CommonJS: require with promise!', function (done) { 40 | require(['promise!examples/jquery-commonjs'], function (result) { 41 | assert(result); 42 | assert.isString(result); 43 | assert.equal(result, 'jquery-commonjs'); 44 | done(); 45 | }); 46 | }); 47 | 48 | }); 49 | 50 | suite('Q Promise example', function () { 51 | 'use strict'; 52 | 53 | test('AMD: direct require', function (done) { 54 | require(['examples/q-amd'], function (promise) { 55 | assert(promise); 56 | assert.isObject(promise); 57 | assert.isFunction(promise.then); 58 | done(); 59 | }); 60 | }); 61 | 62 | test('AMD: require with promise!', function (done) { 63 | require(['promise!examples/q-amd'], function (result) { 64 | assert(result); 65 | assert.isString(result); 66 | assert.equal(result, 'q-amd'); 67 | done(); 68 | }); 69 | }); 70 | 71 | test('CommonJS: direct require', function (done) { 72 | require(['examples/q-commonjs'], function (promise) { 73 | assert(promise); 74 | assert.isObject(promise); 75 | assert.isFunction(promise.then); 76 | done(); 77 | }); 78 | }); 79 | 80 | test('CommonJS: require with promise!', function (done) { 81 | require(['promise!examples/q-commonjs'], function (result) { 82 | assert(result); 83 | assert.isString(result); 84 | assert.equal(result, 'q-commonjs'); 85 | done(); 86 | }); 87 | }); 88 | 89 | }); 90 | 91 | suite('RSVP Promise example', function () { 92 | 'use strict'; 93 | 94 | test('AMD: direct require', function (done) { 95 | require(['examples/rsvp-amd'], function (promise) { 96 | assert(promise); 97 | assert.isObject(promise); 98 | assert.isFunction(promise.then); 99 | done(); 100 | }); 101 | }); 102 | 103 | test('AMD: require with promise!', function (done) { 104 | require(['promise!examples/rsvp-amd'], function (result) { 105 | assert(result); 106 | assert.isString(result); 107 | assert.equal(result, 'rsvp-amd'); 108 | done(); 109 | }); 110 | }); 111 | 112 | test('CommonJS: direct require', function (done) { 113 | require(['examples/rsvp-commonjs'], function (promise) { 114 | assert(promise); 115 | assert.isObject(promise); 116 | assert.isFunction(promise.then); 117 | done(); 118 | }); 119 | }); 120 | 121 | test('CommonJS: require with promise!', function (done) { 122 | require(['promise!examples/rsvp-commonjs'], function (result) { 123 | assert(result); 124 | assert.isString(result); 125 | assert.equal(result, 'rsvp-commonjs'); 126 | done(); 127 | }); 128 | }); 129 | 130 | }); 131 | 132 | if (this.Promise) { 133 | 134 | suite('ES6 Promise example', function () { 135 | 'use strict'; 136 | 137 | test('AMD / CommonJS: direct require', function (done) { 138 | require(['examples/es6'], function (promise) { 139 | assert(promise); 140 | assert.isObject(promise); 141 | assert.isFunction(promise.then); 142 | done(); 143 | }); 144 | }); 145 | 146 | test('AMD / CommonJS: require with promise!', function (done) { 147 | require(['promise!examples/es6'], function (result) { 148 | assert(result); 149 | assert.isString(result); 150 | assert.equal(result, 'es6'); 151 | done(); 152 | }); 153 | }); 154 | 155 | }); 156 | 157 | } 158 | 159 | suite('CommonJS combination example', function () { 160 | 'use strict'; 161 | 162 | test('direct require', function (done) { 163 | require(['examples/combination-commonjs'], function (promise) { 164 | assert(promise); 165 | assert.isObject(promise); 166 | assert.isFunction(promise.then); 167 | done(); 168 | }); 169 | }); 170 | 171 | test('require with promise!', function (done) { 172 | require(['promise!examples/combination-commonjs'], function (result) { 173 | assert(result); 174 | assert.isArray(result); 175 | assert.sameMembers(result, [ 176 | 'jquery-amd', 177 | 'jquery-commonjs', 178 | 'q-amd', 179 | 'q-commonjs', 180 | 'rsvp-amd', 181 | 'rsvp-commonjs' 182 | ]); 183 | done(); 184 | }); 185 | }); 186 | 187 | }); 188 | 189 | suite('AMD combination example', function () { 190 | 'use strict'; 191 | 192 | test('direct require', function (done) { 193 | require(['examples/combination-amd'], function (promise) { 194 | assert(promise); 195 | assert.isObject(promise); 196 | assert.isFunction(promise.then); 197 | done(); 198 | }); 199 | }); 200 | 201 | test('require with promise!', function (done) { 202 | require(['promise!examples/combination-amd'], function (result) { 203 | assert(result); 204 | assert.isArray(result); 205 | assert.sameMembers(result, [ 206 | 'jquery-amd', 207 | 'jquery-commonjs', 208 | 'q-amd', 209 | 'q-commonjs', 210 | 'rsvp-amd', 211 | 'rsvp-commonjs' 212 | ]); 213 | done(); 214 | }); 215 | }); 216 | 217 | }); 218 | 219 | -------------------------------------------------------------------------------- /test/unminified.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 29 | 30 | 31 | --------------------------------------------------------------------------------