I have an async function that calls another async function using await but the first function does not wait for the second function to complete.
module.exports.checkNewUser = async(uuid) => {
const getUserQuery = `MATCH (n:USER {uuid:"${uuid}"}) RETURN n;`
console.log(getUserQuery)
console.log('1')
const result = await this.runCypherQuery(getUserQuery)
console.log('3')
console.log('results:')
console.log(JSON.stringify(result, null, indent))
if(result !== []) throw new Error('user already exists')
return
}
const request = require('request')
const uri = 'http://localhost:7474/db/data/transaction/commit'
module.exports.runCypherQuery = async(query) => {
const headers = { Authorization: 'Basic xxx' }
const json = {statements: [{statement: query, parameters: {}}]}
//console.log(json)
request.post({uri: uri, headers: headers, json: json}, (err, res, body) => {
if(err) throw new Error(err.message)
//console.log(body)
const data = body.results[0].data
console.log('data:')
console.log(data)
console.log('2')
return data
})
}
The terminal output is:
MATCH (n:USER {uuid:"xxx"}) RETURN n;
1
3
results:
undefined
data:
[]
2