// ######################################################################## // PROLOGUE - LICENSE /* Copyright (c) 2013 by Greg Reimer https://github.com/greim http://obadger.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ (function(){ // ------------------------------------------------------------------------ // CLOSURE SCOPE VARS var slice = Array.prototype.slice; // ------------------------------------------------------------------------ // PROGRESS CLASS function Progress(){} Progress.prototype = { getAverage: function(){ var names = Object.keys(this); var total = 0; for (var i=0; i 0; } // Populate slots. items.forEach(function(item) { if (item instanceof Promise) { Object.keys(item._slots).forEach(function(item) { this._slots[item] = false; }, this); } else { this._slots[item] = false; } }, this); // Having populated slots, take promises. items.forEach(function(item) { if (item instanceof Promise) { this.take(item); } }, this); Object.keys(this._slots).forEach(function(slot){ this._progress[slot] = 0; }, this); return this; } }; // ------------------------------------------------------------------------ // FACTORY FUNCTION // this is the function exported var await = function(){ var prom = new Promise(); prom._buildState.apply(prom, arguments); return prom; }; // ------------------------------------------------------------------------ // AWAITING LISTS await.all = function(list) { if (!list || list.length === 0) { return await('length').keep('length',0); } var keys = list.map(function(prom, idx){ return idx; }); keys.push('length'); return await.apply(this, keys).run(function(allProm){ allProm.keep('length', list.length); list.forEach(function(prom, idx){ prom.onfail(allProm.failer()); prom.onkeep(function(got){ allProm.keep(idx, got); }); }); }); }; // ------------------------------------------------------------------------ // INSTANCEOF SUPPORT // so that "foo instanceof await" works await.prototype = Promise.prototype; // ------------------------------------------------------------------------ // EXPORT // for browsers try { if (typeof define === 'function' && define.amd) { define('await', [], function(){ return await; }); } else { window.await = await; } } catch(err) {} // for node try { module.exports = await; // back compat, for people calling this lib // like var await = require('await').await module.exports.await = await; } catch(err) {} })();