0

Hello how can I write the following code so that there is no race condition if I return inside the get() function it only returns from that function but if I return from the outer function it prematurely returns.

function checkifvalid (userIdPassed) {
  // code to be executed
  var params43 = {
    TableName: 'users',
    Key: {
      'pid': req.user.iden
    }
  }
  var returnVal = null
  docClient.get(params43, function (err43, data43) {
    if (err43) {
      return res.json({'errasdsd342sd': 'erhf32623hrf'})
    } else {
      if (data43.Item.useract && data43.Item.curadmin != '0') {
        returnVal =  true
      } else {
        returnVal = false
      }
    }})

  if (returnVal !== null) {
    return returnVal
  }

}
Leo
  • 81
  • 1
  • 2
  • 6
  • `if I return inside the get() function it only returns from that function` Why do you need to return from anywhere else but inside the get function? – KayakinKoder Jan 19 '18 at 04:24
  • 1
    as you've tagged `promise`, so, here's a suggestion using `Promise`, - wrap the code in a `return new Promise` which you resolve or reject in the `docClient.get` callback - of course, now checkifvalid will return said `Promise`, so use appropriate methods to access the result (`.then` or `async/await`) – Jaromanda X Jan 19 '18 at 04:50
  • 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) – ponury-kostek Jan 19 '18 at 13:46

0 Answers0