Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to input Promise string value into an object

In the code below

const getOptionName = async(id): Promise<void> => {
  const data = await dealStore.findById(id)
  console.log('data.optionName', data.optionName)
  console.log('typeof(data.optionName)', typeof(data.optionName))

  return data.optionName
}

const openRemoveDialog = (): void => {
  const checkedItems = grid1.current.getCustomCheckedList().map(item => {
    return {
      optionName: getOptionName(item.id),
      channelName: item?.channelName,
      name: item?.name,
      id: item.id,
    }
  })
  console.log('checkedItems', checkedItems)
}

My console.log('data.optionName', data.optionName) outputs

data.optionName 딜 옵션명 테스트

My console.log('typeof(data.optionName)', typeof (data.optionName)) outputs

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

typeof(data.optionName) string

My console.log('checkedItems', checkedItems) gives the following output

enter image description here

I am confused why my optionName value is a Promise instead of a string when console.log('typeof(data.optionName)', typeof (data.optionName)) shows me that data.optionName is a string

I want my optionName value to be a string and not a Promise. How do I input the PromiseResult string value into my optionName?

>Solution :

getOptionName is an async function, so it will return a promise. To get the value out of that promise you’ll need to await it. In order to await it your map function needs to be an async function, which means you’re creating an array of promises. You can then use Promise.all to combine the array of promises, and await it to get your finished array of objects.

const openRemoveDialog = async (): void => {
  const promises = grid1.current.getCustomCheckedList().map(async (item) => {
    return {
      optionName: await getOptionName(item.id),
      channelName: item?.channelName,
      name: item?.name,
      id: item.id,
    }
  })
  const checkedItems = await Promise.all(promises);
  console.log('checkedItems', checkedItems)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading