I have a database with text entries. I want to search for the city name, and return the country.
I have an async function which returns a thenable promise.
getText('New').then(alert(result));
After an await
on a 2nd function, I want to resolve the promise with one item from the object found. i.e. TheText .
I have this code:
getText=async()=>{
const TheData=await getDataSet('City','New York'); //{country:'USA',city:'New York}
const TheText=TheData['country']; //'USA'
return new Promise((resolve,reject)=>{
const request=??;
request.onsuccess=()=>{resolve(TheText)};);
}
How can I modify the code above to return TheText as a resolved thenable promise.
Async function already returns a promise, you do not need return another one inside it or you'll get promise in promise
getText = async () => {
//const TheData = await getDataSet('City', 'New York'); //{country:'USA',city:'New York}
//const TheText = TheData['country']; //'USA'
const TheText='USA';
console.log(TheText.constructor.name)
return TheText;
}
res=getText();
console.log(res.constructor.name) //This is already promise
res.then((r) => {
console.log(r)
})
Another solution - just to use your existing promise. As your getDataSet already async function, we can continue with its then() and as it will also return promise - just return it from our function. In this case your function does not need to be async.
getText=()=>{
return getDataSet('City','New York').then((TheData)=>{
return TheData['country'];
})
}