我很难理解为什么我的实现中的Promise.all会在返回数据之前将promise数组视为已解析。
在我的代码中,不包括acquireData方法,但重点是:
我从该函数返回数据(并且日志显示该部分似乎工作正常)
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))
acquireData方法的控制台日志为("获得的结果")
我的期望:
我得到的是什么:
...这使得Promise的.then部分没有任何可用的返回数据。
我错过了一些基本的东西吗?承诺必须多长时间才能返回一个结果并且我只是超过它?每次执行acquireData方法都会很快返回(少于1秒)。
编辑:在下面添加了acquireData:
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;
}
感谢您的帮助。
答案 0 :(得分:0)
acquireData方法本身正在执行对AWS资产的异步调用,但其中没有承诺代码 - 应该有吗?
是的,当然。执行异步操作的函数应该返回该异步操作结果的承诺。你说“我正在从该函数返回数据”但是I doubt you do。