Created
April 24, 2013 18:53
-
-
Save les2/5454567 to your computer and use it in GitHub Desktop.
async api callback - there might be bugs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var asyncApi = asyncApi || (function(win) { | |
| var asyncApi = function(globalName, queueName, createGlobal) { | |
| var global = win[globalName]; | |
| if(!global && createGlobal) { | |
| global = win[globalName] = {}; | |
| } | |
| if(global) { | |
| var queue = global[queueName] || []; | |
| global[queueName] = { | |
| push: function(callback) { | |
| // immediately invoke the callback | |
| callback.call({}); | |
| } | |
| }; | |
| // execute any waiting callback | |
| for(var i = 0; i < queue.length; i++) { | |
| try { | |
| queue[i].call({}); | |
| } catch(err) { /* ignored */ } | |
| } | |
| } | |
| }; | |
| return asyncApi; | |
| })(window); | |
| // Example: | |
| asyncApi('monetajs', 'cmd', false); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// ... finally, the api is loaded, so it runs this code:
asyncApi('theApi', 'q'); // the alert will show now