Cannot Resolve Promise In Object
I'm trying to fetch file and return promise in one method of object and then use this data inside another method of same object: const translator = { currentLanguage: '', g
Solution 1:
You never returned anything from getText()
. Change this:
fetch('js/text.json')
to this:
return fetch('js/text.json')
Also, using the Promise constructor in the second then
callback of getText
is redundant, you can directly return the value:
.then(res => {
console.log(res);
return res;
});
It will, by default, be treated as a resolved promise.
Solution 2:
you forgot to return it
getText() {
return fetch('js/text.json')
.then(res => res.json())
.then(res => {
console.log(res);
return new Promise((resolve) => {
resolve(res);
});
});
}
Post a Comment for "Cannot Resolve Promise In Object"