【发布时间】:2018-02-03 00:20:59
【问题描述】:
我的函数使用了 Promise,但它不能正常工作:
getShirtcolorsCount(){
var params_red ;
var red ;
var params_blue ;
var blue ;
var numb = 0 ;
var docClient = new DynamoDB.DocumentClient();
// Query voor Shirts
params_red = {
TableName: 'ShirtApp',
IndexName: 'Shirt-index',
KeyConditionExpression: 'ShirtC = :sbs AND ShirtQuantity > :snr ' ,
ExpressionAttributeValues: {
':sbs': 'Red' ,
':snr' : numb
}
};
var redPromise = docClient.query(params_red).promise();
redPromise.then(function(data){
console.log('Success');
red = data.Count;
}).catch(function(err) {
console.log(err);
});
params_blue = {
TableName: 'ShirtApp',
IndexName: 'Shirt-index',
KeyConditionExpression: 'ShirtC = :sbs AND ShirtQuantity > :snr ' ,
ExpressionAttributeValues: {
':sbs': 'Blue' ,
':snr' : numb
}
};
var bluePromise = docClient.query(params_blue).promise();
bluePromise.then(function(data){
console.log('Success');
blue = data.Count ; //NEED THAT to add to the array
}).catch(function(err) {
console.log(err);
});
var ShirtInfo = [{
name: 'RedColor',
value: red
}, {
name: 'BlueColor',
value: blue
}];
// **** HERE I NEED HELP what should I PUT in the Promise.all for the array
// I want redPromise and bluePromise to run at the same time after I receive
// data then add then to the array and return the array as the function
Promise.all([redPromise, bluePromise]).then([ShirtInfo])
return ShirtInfo;
}
正如我在 cmets 中添加的那样,我想同时运行 redPromise 和 BluePromise,并在它们从网络接收到数据后,将它们添加到数组中。之后返回该数组。几乎所有东西都只适用于使用Promise.all 的部分。我不知道在.then 后面放什么,所以这些值将被添加到数组中:
Promise.all([redPromise, bluePromise]).then([])
而且我无法弄清楚使用 promise 返回数组的内容。
【问题讨论】:
-
您需要访问
redPromise和bluePromise中Promise.all#then中的resolve值吗? -
格式错误的代码
标签: javascript angular promise amazon-dynamodb aws-sdk-js