From 521cda318b519ef196175fc2107fccea817e2a53 Mon Sep 17 00:00:00 2001 From: Oussama Ben Ghorbel Date: Wed, 5 Jun 2019 15:11:28 +0200 Subject: [PATCH] Adding a function for chained promises loaded scripts. --- index.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 339e4fa..8a0f5ec 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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 }