With the above function, I want to read a term from a dictionary which is written in a JSON file.
function readterm(index){
var s="";
return new Promise( (resolve, reject) => {
fetch('JsonFiles/vocabulary.json')
.then(response => response.json())
.then(data => {
s = data.vocabulary.entry[index].term;
console.log(s);
resolve(s);
}).catch(reject);
});
}
and I call this function, by using the above code:
s1= readterm(5);
console.log(s);
My problem is that when I call this function, the result a have is that:
Promise {<pending>}__proto__: Promise[[PromiseState]]: "fulfilled"[[PromiseResult]]: "apple"
How can I take the promise result and set it to the variable s1, so in this example, the returned value will be s1 = "apple";