0

let say there is one method fetchData which have one promise.

function fetchData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      let data = { pCode: "10001", pName: "orange" };
      resolve(data);
    }, 2000);
  });
}

console.log("program start here");

fetchData().then((res) =>{
    console.log(res)
}).catch((err) =>{
    console.log(err)
}).finally(()=>{
    console.log("program ends here")
})

console.log("all done!!")

actual output as below. Here last console "All Done!!" get executed before "program ends here" -:

program start here
all done!!
{ pCode: '10001', pName: 'orange' }
program ends here

So question is, can we wait till fetchData function done all its taks then print last log "all done !!", something like -:

program start here
{ pCode: '10001', pName: 'orange' }
program ends here
all done!!

why I am expecting this because when I call

const obj= {
    a:  fetchData()
}
console.log(obj)

it prints { a: Promise { } }

and I need something like

{ a:{ pCode: '10001', pName: 'orange' }

I tried below option but non of them working

1.

const obj={
    a: (async() => await fetchData())() // cannot use await without async
}
console.log(obj)

output :
{ a: Promise { <pending> } }
const obj={
    a: fetchData().then((res)=>{
        return res;
      })
}
output :
{ a: Promise { <pending> } }
  • So why not just put the `all done!!` log line right where you currently have the `program ends here` log statement? That would do exactly what you asked for. – Bergi Dec 04 '22 at 00:37
  • good point, actually I need to return data from fetch function. And assume that fetchFunction is calling an API which take some mils. When I return a value and pass to next function it always either 'undefined' or 'Promise'. Also, I'm calling fetchFunction from object literal so cannot use await/async. – Rajiv Sharma Dec 04 '22 at 00:42
  • Your own code already demonstrates how to handle that: `.then((res) => { console.log(res); })`. Here, `console.log` is the next function, you can also call any other function in there with the `res`ult. – Bergi Dec 04 '22 at 00:43
  • "*I'm calling fetchFunction from object literal so cannot use await/async.*" - not sure what you mean, object literals have nothing to with whether you can use `await` or not. Please show your actual code in the question then if you have trouble with the syntax, in the code you have posted it would be easily possible. Also why did you tag your question with [tag:typescript] and [tag:async-await] if you are using neither of these? Do you want to use them? – Bergi Dec 04 '22 at 00:45
  • let say I call fetch function like this ``` const obj={ a: fetchData().then((res)=>{ console.log(res) }) } ``` it print like ``` { a: Promise { } } { pCode: '10001', pName: 'orange' } ``` I need a value in a property. thanks for your inputs – Rajiv Sharma Dec 04 '22 at 00:50
  • 1
    You should either use `const obj = {a: await fetchData()}; …` or `fetchData().then(res => { const obj = {a: res}; … });`. But please [edit] your question to include these details. – Bergi Dec 04 '22 at 01:07
  • I edited the question. Thanks for suggestions. But still issue not resolve :( I tried below option but non of them working 1. const obj={ a: (async() => await fetchData())() } console.log(obj) output : { a: Promise { } } 2. const obj={ a: fetchData().then((res)=>{ return res; }) } output : { a: Promise { } } – Rajiv Sharma Dec 04 '22 at 01:29
  • Why did you not use the expressions I was suggesting? You need to put the async handling *around* the construction of the object literal, not inside it. – Bergi Dec 04 '22 at 03:06
  • thanks @Bergi !! Fixed the issue following the link https://stackoverflow.com/questions/46515764/how-can-i-use-async-await-at-the-top-level. – Rajiv Sharma Dec 04 '22 at 04:14

0 Answers0