0

Yes I already read some of the answers here but I somehow could not solve my problem which is the following:

I need to wait for the result of an async call.

function clientSideFunction(){ // cannot be async
  const waitForResult = callServer(); // no await here because clientSidefunction cannot be async
  return waitForResult; // must be called after callServer 
}

async function callServer() { // need to get rid of async keyword
  const result = await new Promise((resolve, reject) => Meteor.call('someServerSideFunction',            (error, result) => {
    if (error) return reject(error);
    resolve(result);
  }));
  return result;
}

I hope it's clear what I try to achieve.

Basically, I want to wait for the result to be ready before I continue the code execution after calling the callServer function.

It must not be async-await if there are other ways (promises).

Ndifreke
  • 732
  • 1
  • 8
  • 13
Gobliins
  • 3,848
  • 16
  • 67
  • 122
  • Did you try using `then` and callbacks? – k0pernikus Jul 18 '19 at 10:40
  • And why do you want to get rid of the async keyword? – k0pernikus Jul 18 '19 at 10:41
  • 2
    "Basically i want to wait for the result to be ready, before i continue the code execution after calling the callServer function" — You can't. That is what async means! – Quentin Jul 18 '19 at 10:41
  • 1
    "*clientSidefunction cannot be async*" - why not? And if it really can't, then that means that you cannot call async functions such as `callServer` from it. – Bergi Jul 18 '19 at 10:42
  • @k0pernikus i cannot return `waitforResult` inside `.then` function? – Gobliins Jul 18 '19 at 10:43
  • The return value of a function passed to `then` is adopted as the resolution of the promise. Outside of the promise chain, you still have the promise and not the value. – Quentin Jul 18 '19 at 10:44
  • @k0pernikus, Bergi: because i can't make the outer function async – Gobliins Jul 18 '19 at 10:45
  • 1
    You can't have the resolved value of a promise before the promise resolves. This is a constraint imposed by living in a universe with linear time. – Quentin Jul 18 '19 at 10:45
  • clientSidefunction should be async. how can you get result from sync method if you are calling async method internally . you will not get result if you use sync method. because you are calling async method inside sync. – Nitesh singh Jul 18 '19 at 10:46
  • @Quentin that's why i want to wait until it resolves.... – Gobliins Jul 18 '19 at 10:46
  • 1
    @Gobliins — You can't. It doesn't work that way. – Quentin Jul 18 '19 at 10:48
  • @Quentin Then how can you control race conditions? – Gobliins Jul 18 '19 at 10:52
  • https://forums.meteor.com/t/race-conditions-on-multiple-instances-of-meteor-accessing-mongodb/37795/2 – Quentin Jul 18 '19 at 10:53
  • https://stackoverflow.com/questions/18007014/meteor-could-a-race-condition-happen-with-meteor-collections-on-server-side – Quentin Jul 18 '19 at 10:54

0 Answers0