0

I'm trying to make a DB request before all my other code.

Heres what I have so far:

 (() => {
    const initParams = {
    TableName: table,
    Key: {
      'userId': from.toString(),
    }
  };
 dynamo.get(initParams, function (err, data) {
   if (err || isEmpty(data)) {
     console.log("INIT ERROR ----------------------------------", 
     JSON.stringify(err, null, 2));
     firstTimeUser = true;
  } else {
    console.log("INIT SUCCESS---------------------------------", 
    JSON.stringify(data, null, 2));
    firstTimeUser = false;
    //user exists
    userData = data.Item;
   }
 });
})();

But: My subsequent code is executed before the IFEE is finished, and thus has no data.

How do I wait until the data has arrived before proceeding?

haayhappen
  • 11
  • 3
  • 6
    The IIFE is *not* asnychronous (and that's a problem). `dynamo.get` is asynchronous. Make a promise for that, return it, and chain onto it. – Bergi Sep 27 '19 at 15:13
  • Please see [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992), which this duplicates. – Dave Newton Sep 27 '19 at 15:14
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ggorlen Sep 27 '19 at 15:18

0 Answers0