异步函数节点js?

时间:2018-05-20 13:51:36

标签: javascript node.js asynchronous promise

我试图了解如何在节点js中使用异步函数。我是初学者,希望对我所面临的这个错误有一些指导。我正在从我的函数执行POST请求并将数据推送到数组中。想法是在循环计数器完成后发送数组。但是,出于某种原因,我最终在执行post请求之前接收到空数组。我该如何解决这个错误?

    function checkforcurrency(vals,basecurrency,targetcurrency) {

        var cval = []

        var currencyexchange = basecurrency + "_" + targetcurrency;


        return new Promise(
            function (resolve, reject) {

                for (var i = 0; i < vals.length; i++) {

                    split_string = vals[i].split(/(\d+)/)
                    var currency_determine = split_string[0]
                    var currency_value = parseFloat(split_string[1])
                    console.log('currency', currency_determine)
                    var cexchange = basecurrency + "_" + targetcurrency;


                    if (currency_determine.length < 2) {


                        request.get('http://free.currencyconverterapi.com/api/v5/convert?q=' + cexchange + '&compact=y', function (err, res, body) {

                            var s = JSON.parse(body)
                            console.log('sss', s)
                            console.log('cjdjdkkdkd', s[cexchange])

                            baseconversionrate = parseFloat(s[cexchange].val);
                            console.log('cvb', baseconversionrate)

                            var finalres = currency_value * baseconversionrate;
                            console.log('kkk', finalres)
                            if (finalres != null) {
                                console.log('hdhdhhdhdhdhdhdhhd', finalres)
                                cval.push(finalres);

                            }


                        })


                    }
                    if (currency_determine.length > 2) {

                        var currencyexchange = currency_determine + "_" + targetcurrency;
                        request.get('http://free.currencyconverterapi.com/api/v5/convert?q=' + currencyexchange + '&compact=y', function (err, res, body) {

                            var s = JSON.parse(body)
                            console.log('sss', s)
                            console.log('ckkdkd', s[currencyexchange])

                            conversionrate = parseFloat(s[currencyexchange].val);
                            var finalres = vals[i] * conversionrate;


                            cval.push(finalres);


                        });


                    }


                }

                setTimeout(resolve, 120000)


                console.log('cva;', cval)
                resolve(cval)


            })


    }


app.post('/financialtrading', function(req, res) {

    console.log('Called',req.body)

    var rev =req.body.revenue;

    var profit = req.body.profit;
    var grossincome = req.body.grossincome;

    var basecurrency = req.body.basecurrency;

    var targetcurrency = req.body.targetcurrency;




       checkforcurrency(rev,basecurrency,targetcurrency).then(function (result) {

           var resu = {"res":result}
           res.send(resu)

        });











});

0 个答案:

没有答案