I have function:
var lang
lang = "nothing"
let url = './' version '/' name '.json'
fetch(url)
.then(response => response.text())
.then((data) => {setLangToData(data)})
}
and
function setLangToData(data) {
lang = data
}
and i want to run getLangFile("en_gb", "1.18"), and when it get the data, run getLangFile("de_de", "1.18") and then someAnotherFunction()
I know solution is something with callback or promises, but i don't understand it and i wanna learn it later. I have simple beginner project, but it can't work without this. Thanks for any help
CodePudding user response:
async function test(){
const res1 = await getLangFile("en_gb", "1.18");
const res2 = await getLangFile("de_de", "1.18");
const res3 = await someAnotherFunction();
}
doc:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
CodePudding user response:
You can try something like this,
const makeHttp = function (url) {
return new Promise(function (resolve, reject) {
fetch(url).then((response) => {
resolve(response.text());
});
});
};
(async function () {
// 1st call
await makeHttp("./version1/name.json");
// 2nd call
await makeHttp("./version2/name.json");
})();
