I'm struggling to understand why Promise.all in my implementation considers the promise array as resolved before data is returned.
In my code, the acquireData method was excluded, but the important points are:
- There is no promise code in the acquireData method - should there be?
- The acquireData method is itself performing async calls to AWS assets... am I supposed to implement additional promises to wait for those? The function call on its own works fine
I am returning the data from that function (and the log shows that part seems to work fine)
let resultArr = event.map((item) => { return new Promise(acquireData(item)) }) Promise.all(resultArr).then(function(values) { console.log("we are about to return the result!") callback(null, values) }).catch(err => callback(err))
The acquireData method has a console log of ("result acquired")
What I expect:
- result acquired
- result acquired
- we are about to return the result!
What I am getting:
- we are about to return the result!
- result acquired
- result acquired
... which makes the .then portion of the Promise not have any of the returned data available.
Am I missing something fundamental? Is there a limit on how long a promise has to return a result and I'm just exceeding it? Each execution of the acquireData method returns fairly quickly (less than 1 second).
EDIT: Added acquireData below:
var acquireData = function(event) {
console.log("Called acquireData!")
console.log("event: ")
console.log(event)
if (event.enterprise_id || (event.sourceKey && event.entity && event.domain)) {
//Search by Enterprise ID
if (event.enterprise_id && event.enterprise_id.length === 36) {
let enterprise_id = event.enterprise_id
let params = {
TableName: 'EDM',
KeyConditionExpression: "#enterprise_id = :enterprise_id",
ExpressionAttributeNames:
{
"#enterprise_id": "enterprise_id"
},
ExpressionAttributeValues:
{
":enterprise_id": enterprise_id
}
}
docClient.query(params, function(err, data) {
if (err)
{
console.log("error on querying enterprise id")
return err
//callback(err, null);
}
else
{
if (data.Items.length === 0)
{
let error = {
"status": 404,
errors: [
{
"code": "NotFoundError",
"source": "database",
"message": "enterprise_id Not found",
"detail": "The enterprise_id provided was not found."
}
]
}
//callback(JSON.stringify(error))
console.log("nothing was found based on enterprise id")
return error
} else {
let result = assembleData(data)
console.log("BEGIN ASSEMBLEDATA RESULT")
console.log(result)
console.log("END ASSEMBLEDATA RESULT")
//callback(null, result)
return result
}
}
})
} else if (event.sourceKey) {
let source_key = event.sourceKey
let domain_entity = `${event.domain}\:${event.entity}`.toUpperCase()
console.log("found a source key record")
console.log(event)
let params = {
TableName: 'EDM',
IndexName: 'source_key-domain-entity-index',
KeyConditionExpression: "#source_key = :source_key and #domain_entity = :domain_entity",
ExpressionAttributeNames:
{
"#source_key": "source_key",
"#domain_entity": "domain-entity"
},
ExpressionAttributeValues:
{
":source_key": source_key,
":domain_entity": domain_entity
}
}
docClient.query(params, function(err, data) {
if (err)
{
console.log("error finding by source key")
return err
//callback(err, null);
}
else
{
if (data.Items.length === 0)
{
let error = {
"status": 404,
errors: [
{
"code": "NotFoundError",
"source": "database",
"message": "enterprise_id Not Found",
"detail": "No enterprise_id could be found with the sourceKey and domain/entity combination provided."
}
]
}
//callback(JSON.stringify(error))
console.log("404 source key not found")
return error
} else {
let result = assembleData(data)
let params = {
TableName: 'EDM',
KeyConditionExpression: "#enterprise_id = :enterprise_id",
ExpressionAttributeNames:
{
"#enterprise_id": "enterprise_id"
},
ExpressionAttributeValues:
{
":enterprise_id": result.enterprise_id
}
}
docClient.query(params, function(err, data) {
if (err)
{
//callback(err, null);
return err
}
else
{
if (data.Items.length === 0)
{
let error = {
"status": 404,
errors: [
{
"code": "NotFoundError",
"source": "database",
"message": "enterprise_id Not found",
"detail": "The enterprise_id provided was not found."
}
]
}
//callback(JSON.stringify(error))
console.log("this error should never be hit... it means it found an enterprise id based on provided source key and then didn't find the eid right after that")
return error
} else {
let result = assembleData(data)
//callback(null, result)
console.log("we are about to get a result!")
console.log(result)
return result
}
}
})
}
}
})
} else {
let error = {
"status": 400,
errors: [
{
"code": "InvalidParameterError",
"source": "enterprise_id",
"message": "Value in unexpected format",
"detail": "enterprise_id was in unexpected format. Ensure that the string provided follows the GUID format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
}
]
}
//callback(JSON.stringify(error))
return error
}
} else {
//callback("The enterprise_id or sourceKey and domain/entity must be provided.")
return "The enterprise_id or sourceKey and domain/entity must be provided."
}//endif event.enterprise_id
}
var assembleData = function(data) {
var source_keys = [];
data.Items.forEach((item) => {
source_keys.push({ "domain-entity": item["domain-entity"], "source_key": item.source_key, "date_modified": item._modified })
})
var response = {
enterprise_id: data.Items[0].enterprise_id,
source_keys: source_keys
}
//console.log(JSON.stringify(response))
return response;
}
Thanks for your help.