Last active
November 30, 2016 20:59
-
-
Save blasten/8f41e5afb36fa1e0cf2b794b14460321 to your computer and use it in GitHub Desktop.
async-lib-pattern
This file contains 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 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