{"action":"created","issue":{"url":"https://api.github.com/repos/jrburke/requirejs/issues/1436","labels_url":"https://api.github.com/repos/jrburke/requirejs/issues/1436/labels{/name}","comments_url":"https://api.github.com/repos/jrburke/requirejs/issues/1436/comments","events_url":"https://api.github.com/repos/jrburke/requirejs/issues/1436/events","html_url":"https://github.com/jrburke/requirejs/issues/1436","id":111862211,"number":1436,"title":"FR: let `require([...deps])` returns a Promise","user":{"login":"herby","id":222129,"avatar_url":"https://avatars.githubusercontent.com/u/222129?v=3","gravatar_id":"","url":"https://api.github.com/users/herby","html_url":"https://github.com/herby","followers_url":"https://api.github.com/users/herby/followers","following_url":"https://api.github.com/users/herby/following{/other_user}","gists_url":"https://api.github.com/users/herby/gists{/gist_id}","starred_url":"https://api.github.com/users/herby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/herby/subscriptions","organizations_url":"https://api.github.com/users/herby/orgs","repos_url":"https://api.github.com/users/herby/repos","events_url":"https://api.github.com/users/herby/events{/privacy}","received_events_url":"https://api.github.com/users/herby/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-16T16:10:37Z","updated_at":"2015-10-17T00:28:22Z","closed_at":null,"body":"If callbacks are not provided (or maybe even when they are), the `require([...deps])` call itself should return a promise that is resolved or rejected by array of args that would go to callbacks themselves.\r\n\r\nAdditionally, global `Promise` should be reused - so promise is only returned when its constructor is present (otherwise, it should probably return `null`).\r\n\r\nWrapping the call itself to Promise is not as hard, in fact something like:\r\n\r\n```js\r\nrequire = function (deps) {\r\n  return typeof Promise !== \"function\" ?\r\n    _require.apply(this, arguments) :   \r\n    new Promise(function (resolve, require) {\r\n      _require(deps,\r\n        function () { resolve([].slice.call(arguments)); },\r\n        function () { reject([].slice.call(arguments)); });\r\n    });\r\n};\r\n```\r\n\r\nwhen creating the `require` should be enough given `_require` is current, promiseless version of require."},"comment":{"url":"https://api.github.com/repos/jrburke/requirejs/issues/comments/148870519","html_url":"https://github.com/jrburke/requirejs/issues/1436#issuecomment-148870519","issue_url":"https://api.github.com/repos/jrburke/requirejs/issues/1436","id":148870519,"user":{"login":"amenadiel","id":238439,"avatar_url":"https://avatars.githubusercontent.com/u/238439?v=3","gravatar_id":"","url":"https://api.github.com/users/amenadiel","html_url":"https://github.com/amenadiel","followers_url":"https://api.github.com/users/amenadiel/followers","following_url":"https://api.github.com/users/amenadiel/following{/other_user}","gists_url":"https://api.github.com/users/amenadiel/gists{/gist_id}","starred_url":"https://api.github.com/users/amenadiel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amenadiel/subscriptions","organizations_url":"https://api.github.com/users/amenadiel/orgs","repos_url":"https://api.github.com/users/amenadiel/repos","events_url":"https://api.github.com/users/amenadiel/events{/privacy}","received_events_url":"https://api.github.com/users/amenadiel/received_events","type":"User","site_admin":false},"created_at":"2015-10-17T00:28:22Z","updated_at":"2015-10-17T00:28:22Z","body":"As a proof of concept I declared the following method:\r\n\r\n```js\r\nrequirejs.when = function () {\r\n\r\n\t\tvar deferred = new jQuery.Deferred(),\r\n\t\t\tflat_arguments = _.flatten(arguments);\r\n\r\n\t\trequire(flat_arguments, function () {\r\n\t\t\tdeferred.resolve.apply(null, arguments);\r\n\t\t});\r\n\r\n\t\treturn deferred.promise();\r\n\t};\r\n```\r\n\r\nThen I tried with:\r\n\r\n```js\r\n requirejs\r\n    .when([ \"vendor/lodash.min\",  \"vendor/backbone\"], \"vendor/qunit\")\r\n    .then(function(lodash,bbone,qunit) {  \r\n        console.debug({\r\n            'lodash':lodash,\r\n           'backbone':bbone,\r\n           'qunit':qunit\r\n       }); \r\n   });\r\n```\r\n\r\nand it works. The arguments flattening allows for the method to be called with\r\n\r\n```js\r\nrequirejs.when([\"modA\",\"modB\"],\"modC\")\r\n```\r\n\r\nor\r\n\r\n```js\r\nrequirejs.when([\"modA\",\"modB\",\"modC\"])\r\n```\r\n\r\nor\r\n\r\n```js\r\nrequirejs.when(\"modA\",\"modB\",\"modC\")\r\n```\r\n\r\nit could be further enhanced by\r\n- not relying in jquery or lodash as in this example\r\n- accepting actual promises among the parameters, to mix both async AMD calls as well as other promises to be called at once. "}}