对循环中的承诺感到困惑
Confused with promises in a loop
我正在编写执行以下操作的 dynamodb 代码。
- 扫描特定列并获取值并将其添加到数组
- 将数组转换为集合并返回数组以获得唯一值
- 循环遍历此设置值作为参数并获取实际值。
基本上是尝试在 DynamoDb 中创建一个组。
第一步和第二步我都能做到。但是到了第 3 步,我有一个循环,在循环中必须执行代码,我的代码如下。
var AWS = require("aws-sdk");
var creds = new AWS.Credentials('akid', 'secret', 'session');
AWS.config.update({
"accessKeyId": "myAccessId",
"secretAccessKey": "MySecretAccessKey",
"region": "us-east-1"
});
var dynamodb = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "MyTable",
FilterExpression: "#target_state = :target_state",
ExpressionAttributeNames: {
"#target_state": "target_state"
},
ExpressionAttributeValues: {
":target_state": "5"
}
};
var array = [];
dynamodb.scan(params).promise().then(function (data) {
data.Items.forEach(function (itemData) {
array.push(itemData.ruleNo)
});
console.log(array);
return array;
}).then(() => {
console.log("Entered 2nd block " + [...new Set(array)]);
var array2 = [...new Set(array)];
for (index = 0; index < array2.length; ++index) {
console.log(array2[index]);
var params1 = {
TableName: "ChemicalData",
FilterExpression: "#target_state = :target_state and #ruleNo=:ruleNo",
ExpressionAttributeNames: {
"#target_state": "target_state",
"#ruleNo": "ruleNo"
},
ExpressionAttributeValues: {
":target_state": "5",
":ruleNo": array2[index]
}
};
return dynamodb.scan(params1).promise().then(function (data) {
var uw = JSON.stringify((data.Items));
return uw;
});
}
}).then((data) => {
console.log(data);
}).catch(err => {
console.log(err)
})
当我运行这个程序时,我得到的结果只有一个值,那是第一个数组值,我不知道如何遍历所有数组变量和然后做一个console.log(data)
。请让我知道我哪里出错了,我该如何解决这个问题。
谢谢
在 for 中使用 return
来打破循环。您应该将内部扫描的承诺收集到数组中,然后使用 Promise.all
一起解析
dynamodb.scan(params).promise().then(function (data) {
data.Items.forEach(function (itemData) {
array.push(itemData.ruleNo)
});
console.log(array);
return array;
}).then(() => {
console.log("Entered 2nd block " + [...new Set(array)]);
var array2 = [...new Set(array)];
var results = []; //results array
for (index = 0; index < array2.length; ++index) {
console.log(array2[index]);
var params1 = {
TableName: "ChemicalData",
FilterExpression: "#target_state = :target_state and #ruleNo=:ruleNo",
ExpressionAttributeNames: {
"#target_state": "target_state",
"#ruleNo": "ruleNo"
},
ExpressionAttributeValues: {
":target_state": "5",
":ruleNo": array2[index]
}
};
// push results to be resolved later
results.push(dynamodb.scan(params1).promise().then(function (data) {
var uw = JSON.stringify((data.Items));
return uw;
}));
}
// return promise that resolves when all results resolve
return Promise.all(results);
})
我正在编写执行以下操作的 dynamodb 代码。
- 扫描特定列并获取值并将其添加到数组
- 将数组转换为集合并返回数组以获得唯一值
- 循环遍历此设置值作为参数并获取实际值。
基本上是尝试在 DynamoDb 中创建一个组。
第一步和第二步我都能做到。但是到了第 3 步,我有一个循环,在循环中必须执行代码,我的代码如下。
var AWS = require("aws-sdk");
var creds = new AWS.Credentials('akid', 'secret', 'session');
AWS.config.update({
"accessKeyId": "myAccessId",
"secretAccessKey": "MySecretAccessKey",
"region": "us-east-1"
});
var dynamodb = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "MyTable",
FilterExpression: "#target_state = :target_state",
ExpressionAttributeNames: {
"#target_state": "target_state"
},
ExpressionAttributeValues: {
":target_state": "5"
}
};
var array = [];
dynamodb.scan(params).promise().then(function (data) {
data.Items.forEach(function (itemData) {
array.push(itemData.ruleNo)
});
console.log(array);
return array;
}).then(() => {
console.log("Entered 2nd block " + [...new Set(array)]);
var array2 = [...new Set(array)];
for (index = 0; index < array2.length; ++index) {
console.log(array2[index]);
var params1 = {
TableName: "ChemicalData",
FilterExpression: "#target_state = :target_state and #ruleNo=:ruleNo",
ExpressionAttributeNames: {
"#target_state": "target_state",
"#ruleNo": "ruleNo"
},
ExpressionAttributeValues: {
":target_state": "5",
":ruleNo": array2[index]
}
};
return dynamodb.scan(params1).promise().then(function (data) {
var uw = JSON.stringify((data.Items));
return uw;
});
}
}).then((data) => {
console.log(data);
}).catch(err => {
console.log(err)
})
当我运行这个程序时,我得到的结果只有一个值,那是第一个数组值,我不知道如何遍历所有数组变量和然后做一个console.log(data)
。请让我知道我哪里出错了,我该如何解决这个问题。
谢谢
在 for 中使用 return
来打破循环。您应该将内部扫描的承诺收集到数组中,然后使用 Promise.all
一起解析
dynamodb.scan(params).promise().then(function (data) {
data.Items.forEach(function (itemData) {
array.push(itemData.ruleNo)
});
console.log(array);
return array;
}).then(() => {
console.log("Entered 2nd block " + [...new Set(array)]);
var array2 = [...new Set(array)];
var results = []; //results array
for (index = 0; index < array2.length; ++index) {
console.log(array2[index]);
var params1 = {
TableName: "ChemicalData",
FilterExpression: "#target_state = :target_state and #ruleNo=:ruleNo",
ExpressionAttributeNames: {
"#target_state": "target_state",
"#ruleNo": "ruleNo"
},
ExpressionAttributeValues: {
":target_state": "5",
":ruleNo": array2[index]
}
};
// push results to be resolved later
results.push(dynamodb.scan(params1).promise().then(function (data) {
var uw = JSON.stringify((data.Items));
return uw;
}));
}
// return promise that resolves when all results resolve
return Promise.all(results);
})