-2

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.

5
  • It's not clear to me what you're trying to accomplish. Once you have the value TheText, wouldn't your getText function simply return it? What is the purpose of constructing a Promise at that point? Can you clarify the underlying problem you're trying to solve? Commented May 12 at 10:55
  • "Do something" isn't particularly specific. What do you need to do with the result? Can you provide a more complete example of the problem? Commented May 12 at 10:58
  • What isn't clear is why you need to create an additional Promise at all. If the goal is to return TheText from the function then why not simply return TheText; ? Commented May 12 at 11:05
  • If I use "await" then the calling function needs to be async. And the function calling that function needs to be async. etc That's correct and cannot be avoided. It's promises all the way down. Commented May 12 at 11:11
  • @Cymro: What is GetValue? Where are you awaiting that? Do you mean getText? If you can't use await on it then you can follow it with a .then() callback, exactly like the code you've already written demonstrates. So... why doesn't that work for you? What's the actual problem you encounter? Commented May 12 at 11:11

1 Answer 1

1

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'];
    })
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sergey, return .then return. 2 returns, correct?
Do not have your function, but I think it is something like fetch? In this case yes. The return text inside .then() wil create a promise with resolving this text. The outer return - for receiving this promise from getText() in your further code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.