在for循环中使用Promises JavaScrMipt nodeJS

时间:2018-05-24 17:54:03

标签: node.js for-loop azure-sql-database es6-promise

我试图在for循环上使用Promise函数,但我一直在获取null, 我成功地用Promise.ALL解决了这个问题并且正在处理我的三个对象,但是我需要编写一个包含超过50个对象的函数,这个解决方案看起来并不合适。 / p>

建议?感谢。

承诺功能

function getPOIbyID(id) {
    return new Promise(function (resolve, reject) {
        DButilsAzure.execQuery("select * from POI where ID='" + id + "'").then(function (responsePOI) {
            let poi = responsePOI[0];
            return DButilsAzure.execQuery("select Rank, body, Date from ReviewsPoi where POIid=" + id + " order by Date desc").then(function (responseREV) {
                let rev = responseREV;
                let lastTwo = [];
                for (let i = 0; (i < responseREV.length && i < 2); i++) {
                    lastTwo[i] = responseREV[i];
                }
                let returnPOI = {
                    "Name": poi.Name,
                    "Description": poi.Description,
                    "Rank": poi.Rank,
                    "UsersWatching": poi.UserWaching,
                    "Picture": poi.Picture,
                    "Reviews": lastTwo
                }
                resolve(returnPOI);
            }).catch(function (err) {console.log (err) })
        }).catch(function (err) { console.log (err)})
    });
}

初始代码:

router.get('/Random3', function (req, res) {
    DButilsAzure.execQuery("select ID from POI").then(function (responseID) {
        let retArr = [];
        for (let i=0; i<3; i++) {
            let tmpID = Math.floor(Math.random() * (responseID.length));
            getPOIbyID(responseID[tmpID].ID).then(function (responsePOI) {
                    retArr[i] = responsePOI;
                if (i === 2)
                    res.json(retArr);
            }).catch(function (err) { console.log (err)})            
        }
    })
})

解决方案:

router.get('/Random3', function (req, res) {
    DButilsAzure.execQuery("select ID from POI").then(function (responseID) {
        let retArr = [];
        let usedrans=[];
        let count=0;
        var promise1=new Promise(function(resolve,reject) {
            let tmpID = Math.floor(Math.random() * (responseID.length));
            while(usedrans.includes(tmpID))
                tmpID = Math.floor(Math.random() * (responseID.length));
            usedrans[count]=tmpID;
            count++
            resolve(getPOIbyID(responseID[tmpID].ID))
        })
        var promise2=new Promise(function(resolve,reject) {
            let tmpID = Math.floor(Math.random() * (responseID.length));
            while(usedrans.includes(tmpID))
                tmpID = Math.floor(Math.random() * (responseID.length));
            usedrans[count]=tmpID;
            count++
            resolve(getPOIbyID(responseID[tmpID].ID))
        })
        var promise3=new Promise(function(resolve,reject) {
            let tmpID = Math.floor(Math.random() * (responseID.length));
            while(usedrans.includes(tmpID))
                tmpID = Math.floor(Math.random() * (responseID.length));
            usedrans[count]=tmpID;
            count++
            resolve(getPOIbyID(responseID[tmpID].ID))
        })
        Promise.all([promise1,promise2,promise3]).then(function(values){
            res.send(values);
        })
    })

0 个答案:

没有答案