Node js, promises, callback

2

I need to perform certain updates and delete within a return of a query in my controller. but only the last function is executed I do not think I understand how to use promises.

 api.adMaster =  function (req, res) {
        var dados = req.body;
        var ultimo = false;
        var addados = {ad:'', emp:''}
        var time = new Date();
        var promessas = [];
        var resutados = [];

        for (var i = 0; i < dados.length ; i++) {
            if(dados[i].aut.length>2){
                var metade = Math.floor(dados[i].aut.length / 2);
                var resultado = "'"+dados[i].aut.substr(0,metade)+"','"+dados[i].aut.substr(metade)+"'";
                dados[i].aut = resultado
                dados[i].data = convertData(time);
                dados[i].hora = converHora(time);
            }
            addados.ad += ","+dados[i].ad
            addados.emp += ","+dados[i].emp
        }
        addados.ad =  addados.ad.substring(1);
        addados.emp =  addados.emp.substring(1);
        var promessas = [];
        solicitacaoSqlDAO.adMaster(addados, function (erro, recordset) {
            if (erro) {
                console.log("#$# Erro ao adMaster #$#");
                console.log(erro);
            } else {
                var compAD = '';
                var ad;
                console.log( recordset.recordset.length);
                recordset.recordset.forEach(function(data, index) {
                    ad = data;
                    if(compAD==data.NUM_AD){
                        ultimo = true;
                    }
                    if( recordset.recordset.length ==  1){
                        ultimo = true;
                    }
                    if(ultimo || !dados[0].condicao){ 
                        promessas.push(new Promise(function(resolver, rejeitar) {
                            solicitacaoSqlDAO.updateiesSupCapS(ad).then(result=>{
                                resutados.push(result);
                                console.log('updateiesSupCapS');
                                resolver();
                            }).catch(err => {
                                console.log(" @###$$$$##$  ERRO updateiesSupCapS  ###$$$#$#$#$$#");
                                console.log(err);
                                console.log(" @###$$$$##$  ERRO updateiesSupCapS  ###$$$#$#$#$$#");
                            });
                        }));
                        promessas.push(new Promise(function(resolver, rejeitar) {
                            solicitacaoSqlDAO.updateiesSupCapC(ad).then(result=>{
                                resutados.push(result);
                                console.log('updateiesSupCapC');
                                resolver();
                            }).catch(err => {
                                console.log(" @###$$$$##$  ERRO  updateiesSupCapC ###$$$#$#$#$$#");
                                console.log(err);
                                console.log(" @###$$$$##$  ERRO  updateiesSupCapC ###$$$#$#$#$$#");
                            });
                        }));
                        promessas.push(new Promise(function(resolver, rejeitar) {
                            solicitacaoSqlDAO.updateIesLibPgtoCapN(ad).then(result=>{
                                resutados.push(result);
                                console.log('updateIesLibPgtoCapN');
                                resolver();
                            }).catch(err => {
                                console.log(" @###$$$$##$  ERRO  updateIesLibPgtoCapN  ###$$$#$#$#$$#");
                                console.log(err);
                                console.log(" @###$$$$##$  ERRO  updateIesLibPgtoCapN  ###$$$#$#$#$$#");
                            });
                        }));
                        promessas.push(new Promise(function(resolver, rejeitar) {
                            solicitacaoSqlDAO.deleteCapAdSuspAprov(ad).then(result=>{
                                resutados.push(result);
                                console.log('deleteCapAdSuspAprov');
                                resolver();
                            }).catch(err => {
                                console.log(" @###$$$$##$  ERRO  deleteCapAdSuspAprov  ###$$$#$#$#$$#");
                                console.log(err);
                                console.log(" @###$$$$##$  ERRO  deleteCapAdSuspAprov  ###$$$#$#$#$$#");
                            }); 
                        }));
                        ultimo = false;
                        compAD=data.NUM_AD; 


                    }else{
                        res.status(200).json({resutado:'aguardando outro aprovador'});;
                    }
                });
                Promise.all([true, promessas]).then(function(values) {
                    console.log(values);
                    res.status(200).json(resutados);
                }).catch(function() {
                    res.status(404).json("erro");
                });
            }
        })
    };

In my debug in console.log (values) comes this result

[ true,
  [ Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> } ] ]
    
asked by anonymous 01.09.2017 / 14:46

1 answer

1

You're doing fine with Promises but not with Promise.all , this method accepts an array of promises. What you're getting is an array with a boolean and an array.

Use this way:

Promise.all(promessas).then(... etc...

Also notice that at the end, within Promise.all tens

res.status(200).json(resutados);

And I think you want values :

res.status(200).json(values);
    
01.09.2017 / 14:48