Skip to content

Adding a function for chained promises loaded scripts. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function registerLibraryLoaded(id) {
* get a listener and when is loaded first and second will be completed.
* @scenario 3: attempt load the same external script after is completed.
*/
function loadScript({ src, id, data }) {
function getScriptLoadingPromise({ src, id, data }) {
const script = document.createElement('script')

script.id = id
Expand Down Expand Up @@ -55,4 +55,17 @@ function loadScript({ src, id, data }) {
})
}

export default loadScript
function loadScript({ id, src, data }) {
return getScriptLoadingPromise({ id, src, data });
}

function loadScripts(scripts) {
let chain = Promise.resolve();
for (let script of scripts) {
chain = chain.then(() => getScriptLoadingPromise(script))
}
return chain;
}


export { loadScript, loadScripts }