0

我的函数使用了 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;
}

正如我在评论中添加的那样,我想同时运行redPromiseBluePromise在他们从网络接收数据后,将它们添加到数组中。之后返回该数组。几乎所有东西都只在Promise.all使用的部分起作用。我不知道要放什么,.then所以这些值将被添加到数组中:

Promise.all([redPromise, bluePromise]).then([])

而且我无法弄清楚使用承诺返回数组的内容。

4

2 回答 2

0

我的情况是由函数范围变量产生redPromisebluePromise写入函数范围变量,可以像这样推送到数组:

return new Promise(function (resolve, reject) {
  Promise.all([redPromise, bluePromise]).then(function () {
    ShirtInfo.push(red)
    ShirtInfo.push(blue)
    resolve(ShirtInfo)
  })
}

在您调用获取此数组的函数的地方,您还应该使用

getShirtColorsCount().then(function(shirtInfo) {
  // Stuff
})

ps 它会回调地狱。可能是更好的使用babelasync-await/或生成器功能?它会更具可读性

于 2017-08-25T01:45:37.937 回答
0

一些问题:

  • 您需要在回调中返回redand的值,否则这些承诺将解析为.bluethenundefined
  • 同样,您需要返回的返回值Promise.all
  • 您无法访问redblue同步,因为它们仍然是未定义的。所以这必须在then回调中发生。

我还将避免您的代码重复,并使用您感兴趣的颜色列表,然后循环浏览这些颜色:

getShirtcolorsCount(){
    var params;
    var colors;
    var promises;
    var numb = 0;
    var docClient = new DynamoDB.DocumentClient();

    colors = ['Red', 'Blue']; // <--- make code generic
    promises = colors.map(function (color) {
        // Query voor Shirts 
        var param = {
            TableName: 'ShirtApp',
            IndexName: 'Shirt-index',
            KeyConditionExpression: 'ShirtC = :sbs AND ShirtQuantity > :snr ',
            ExpressionAttributeValues: {
                ':sbs': color, // <-- make it dynamic to avoid code duplication
                ':snr' : numb
            }
        };
        return docClient.query(params_red).promise();
    });

    // Make sure to return the promise    
    return Promise.all(promises).then(function (responses) {
        console.log('Success');  
        var shirtInfo = responses.map(function (data, i) {
            return {
                name: color[i] + 'Color',
                value: data.Count
            };
        });
        return shirtInfo;
    }).catch(function(err) {
        console.log(err);
    });
}

一旦你使用了 Promise,你也必须将结果用作 Promise。您不能期望函数同步返回值。因此,当您调用 时getShirtcolorsCount,使用then来访问结果:

getShirtcolorsCount().then(function (shirtInfo) {
    console.log(shirtInfo);
});
于 2017-08-25T01:46:17.007 回答