func1() {
this.setJson().then(json => {
return json;
});
}
func2() {
let q = this.func1();
return q;
}
func1() {
return this.setJson();
}
async func2() {
let q = await this.func1();
return q;
}
const root = 'https://jsonplaceholder.typicode.com';
const func1 = function() {
return fetch(root + '/posts/2').then(response => {
if (response.status === 200) {
return response.json();
} else {
throw new Error("API error")
}
}).then((json) => {
console.log("2",json);
json.fetched = new Date();
return json;
})
}
const func2 = async function() {
let q = await func1();
console.log("1", q)
return q;
}
func2();
getTranslate(key) {
return this.setNewJsonAndGetTranslate(key); <--undefined
}
setNewJsonAndGetTranslate(key) {
this.setJson().then(json => {
let translate = TranslateService.jsonPathToValue(json, key);
let result = JSON.stringify(translate[this.user.getLangCode()]);
if (result !== undefined) {
return result.substring(1, result.length - 1); <-- нужно вернуть
} else {
return key;
}
});
}
setJson() {
return new Promise((resolve, reject) => {
$.getJSON(Environment.prefixPath + "assets/i18n.json", json => {
let translate = JSON.stringify(json);
window.localStorage.setItem("translate_json", translate);
resolve(json);
});
});
}