0

Trying to log rs which should be the value of the function create order but it returns promise pending

let createOrder = async function () {
  let response = await client.execute(request);

  return response
};

let rs = createOrder().then((result) => console.log(result))

console.log("rsssss",rs)
Phil
  • 157,677
  • 23
  • 242
  • 245
  • Isn't it the whole point of async functions? – tromgy Mar 30 '22 at 23:12
  • sure but the point here is .then method isn't giving me a result , so maybe a suggestion what i did wrong . thanks –  Mar 30 '22 at 23:17
  • 2
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Phil Mar 30 '22 at 23:18
  • [Promise.prototype.then()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) always returns a promise – Phil Mar 30 '22 at 23:20
  • thanks but still not the result –  Mar 30 '22 at 23:28
  • 1
    You always have to wait for the promise to resolve. Either with `await` or `.then` – Marc Mar 30 '22 at 23:42

1 Answers1

1

You are not following the correct code pattern to handle async/Promises. When you're chaining methods, the promise is resolved inside the callback, not outside.

The return of .then() method is a promise object.

The correct code is this:


let createOrder = async function () { 
  let response = await client.execute(request);
  return response 
};

createOrder().then((result)=> {
  //the promise is resolved here
  console.log(result)
}).catch(console.error.bind(console))
Jone Polvora
  • 2,184
  • 1
  • 22
  • 33
  • i appreciate it but this actually doesnt show me anything in the console :/ –  Mar 30 '22 at 23:26
  • I edited the code example to catch any errors that maybe is happening. Please show us the code behing `client.execute(request)`, I'm sure that this function is not resolving the promise or it is throwing a exception not handled somewhere. – Jone Polvora Mar 30 '22 at 23:29
  • It is actually a method from the paypal library, a checkout npm –  Mar 30 '22 at 23:33
  • Well, the code I've posted is correct, certainly there's an error somewhere in other lines of your code, like promises not resolving, etc. Have you already tried to run my edited code that will print any error? By the way, we can't guide you more if you not show us more code. – Jone Polvora Mar 30 '22 at 23:38
  • i am actually working with aws , and the crazy part is that when i run the code locally it works , and when i tried it on lambda function it didnt, thats why i made some changes and then decided to ask help from you guys . The rest of the code is not logic is just creation of an order that i am sure its not wrong . i appreciate a lot your help man –  Mar 30 '22 at 23:45