0

I have these 2 sets of code. I am trying to retrieve an app_id in the resolver.

Code A:

let p1 =  new Promise(function(resolve,reject){
axios({
  method: 'POST',
  url: url,
  data: data,
  headers: {'Content-Type': 'application/json' }
})
.then(response => {
  //console.log(response.data)
  final_output = {
    "id":response.data
  }
  resolve(final_output)
})
.catch(error => {
  //console.log("Error")
  reject(error)
});
}) 


p1.then(function(final_output){
   return final_output
})

and

Code B:

return new Promise(function(resolve,reject){
axios({
  method: 'POST',
  url: url,
  data: data,
  headers: {'Content-Type': 'application/json' }
})
.then(response => {
  console.log(response.data)
  final_output = {
    "id":response.data
  }
  resolve(final_output)
})
.catch(error => {
  console.log("Error")
  reject(error)
});
})

I do not understand why Code B would return {"id":1} while Code A would not return {"id:1} to my graphql playground.

Codebling
  • 10,764
  • 2
  • 38
  • 66
  • Given all `axios` calls return their own promises, you do not need `new Promise()` – Phil Mar 30 '20 at 03:33
  • I've tried removing the new Promise() it didn't work and I have no clue why. – user3161830 Mar 30 '20 at 03:35
  • 1
    Both your code samples could be shortened to `return axios.post(url, data).then(({ data }) => ({ id: data }))` – Phil Mar 30 '20 at 03:36
  • You have not shown how you're trying to use this code but I suspect _Code A_ should end in `return p1` (assuming these are both some kind of function body) – Phil Mar 30 '20 at 03:39
  • Thanks, I got it to work by doing this `return p1.then(function(final_output){ return final_output })` in code A – user3161830 Mar 30 '20 at 03:45
  • You seriously don't need that last `then`, it is completely redundant. Did you see my short example above? – Phil Mar 30 '20 at 03:48

0 Answers0