Skip to content

Instantly share code, notes, and snippets.

@blasten
Last active November 30, 2016 20:59
Show Gist options
  • Save blasten/8f41e5afb36fa1e0cf2b794b14460321 to your computer and use it in GitHub Desktop.
Save blasten/8f41e5afb36fa1e0cf2b794b14460321 to your computer and use it in GitHub Desktop.
async-lib-pattern
var libPromise = new Promise(function(resolve) {
window.Lib = window.Lib || [];
window.Lib.push(resolve);
});
libPromise.then(function(lib) {
// ...
});
libPromise.then(function(lib) {
// ...
});
// Also works:
window.Lib = window.Lib || [];
window.Lib.push(function(lib) {
// ...
});
// Lib implementation
const clients = Array.isArray(window.Lib) ? window.Lib : [];
window.Lib = {
version: '1.0',
push(cb) {
cb(this);
}
};
clients.forEach(cb => cb(window.Lib));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment