gpt4 book ai didi

javascript - Node.js 和 Mysql 的 Promise

转载 作者:行者123 更新时间:2023-11-29 18:47:11 25 4
gpt4 key购买 nike

我有一个函数,它的参数中有一个结构体struct,这个结构体是由一些异步SQL请求设置的,如下面的代码所示:

module.exports.loadDatas = function(struct) {

db.queryAsync('SELECT * FROM safeplusdb.vector;')
.then(function(records){
for(i=0; i<records.length; i++)
{
if(records[i].type=="ADSL")
{
struct.labelAdsl=records[i].type;
}
else if (records[i].type=="Cable")
{
struct.labelCable=records[i].type;
}
else if (records[i].type=="Fibre")
{
struct.labelFibre=records[i].type;
}
else if (records[i].type=="Others")
{
struct.labelOthers=records[i].type;
}
else if (records[i].type=="Ott")
{
struct.labelOtt=records[i].type;
}
else if (records[i].type=="Satellite")
{
struct.labelSatellite=records[i].type;
}
else if (records[i].type=="Tnt")
{
struct.labelTnt=records[i].type;
}
}
})
.catch(function(err){
throw err;
});

db.queryAsync("SELECT * FROM safeplusdb.data;")
.then(function (records){
for(i=0; i<records.length; i++)
{
let record= records[i];

if(record.vector_id==0)
{
factorizeSQL(UTCGap, struct.cllForecastAdsl, struct.valueReturnAdsl, record);
}

else if(record.vector_id==1)
{
factorizeSQL(UTCGap, struct.cllForecastCable, struct.valueReturnCable, record);
}

else if(record.vector_id==2)
{
factorizeSQL(UTCGap, struct.cllForecastFibre, struct.valueReturnFibre, record);
}

else if(record.vector_id==3)
{
factorizeSQL(UTCGap, struct.cllForecastOthers, struct.valueReturnOthers, record);
}

else if(record.vector_id==4)
{
factorizeSQL(UTCGap, struct.cllForecastOtt, struct.valueReturnOtt, record);
}

else if(record.vector_id==5)
{
factorizeSQL(UTCGap, struct.cllForecastSatellite, struct.valueReturnSatellite, record);
}

else if(record.vector_id==6)
{
factorizeSQL(UTCGap, struct.cllForecastTnt, struct.valueReturnTnt, record);
}
}
})
.catch(function(err){
throw err;
});

db.queryAsync("SELECT * FROM safeplusdb.admin;")
.then(function (records){
for(i=0; i<records.length; i++)
{
struct.adminList[i]=records[i].name;
}
})
.catch(function(err){
throw err;
});

db.queryAsync("SELECT * FROM safeplusdb.media;")
.then(function (records){
for(i=0; i<records.length; i++)
{
struct.mediaList[i] = { label : records[i].label, value : records[i].value};
}
})
.catch(function(err){
throw err;
});

db.queryAsync("SELECT * FROM safeplusdb.scroll ORDER BY orderscroll;")
.then(function (records){
for(i=0; i<records.length; i++)
{
let record= records[i];

if(record.isscrolled==false)
{
if(record.idm==null && record.vector_id !=null)
{
struct.listUnScroll[struct.indj] = {media : null, platform : null, orderscroll: null};
struct.listUnScroll[struct.indj].orderscroll = record.orderscroll;
struct.listUnScroll[struct.indj].platform = struct.listPlatforms[record.vector_id];
struct.indj++;
}
else if(record.vector_id==null && record.idm!=null)
{
struct.listUnScroll[struct.indj] = {media : null, platform : null, orderscroll: null};
struct.listUnScroll[struct.indj].orderscroll = record.orderscroll;
struct.listUnScroll[struct.indj].media = struct.listMedias[record.idm-1];
struct.indj++;
}
else
{
struct.listUnScroll[struct.indj] = {media : null, platform : null, orderscroll: null};
struct.listUnScroll[struct.indj].orderscroll = record.orderscroll;
struct.listUnScroll[struct.indj].media = struct.listMedias[record.idm-1];
struct.listUnScroll[struct.indj].platform = struct.listPlatforms[record.vector_id];
struct.indj++;
}
}

else if(record.isscrolled==true)
{
if(record.idm==null && record.vector_id !=null)
{
struct.listScroll[struct.indk] = {media : null, platform : null, orderscroll: null};
struct.listScroll[struct.indk].orderscroll = record.orderscroll;
struct.listScroll[struct.indk].platform = struct.listPlatforms[record.vector_id];
struct.indk++;
}
else if(record.vector_id==null && record.idm!=null)
{
struct.listScroll[struct.indk] = {media : null, platform : null, orderscroll: null};
struct.listScroll[struct.indk].orderscroll = record.orderscroll;
struct.listScroll[struct.indk].media = struct.listMedias[record.idm-1];
struct.indk++;
}
else
{
struct.listScroll[struct.indk] = {media : null, platform : null, orderscroll: null};
struct.listScroll[struct.indk].orderscroll = record.orderscroll;
struct.listScroll[struct.indk].media = struct.listMedias[record.idm-1];
struct.listScroll[struct.indk].platform = struct.listPlatforms[record.vector_id];
struct.indk++;
}
}
}
})
.catch(function(err){
throw err;
});

db.queryAsync("SELECT * FROM safeplusdb.device;")
.then(function (records){
for(i=0; i<records.length; i++)
{
struct.deviceList[i] = {value : records[i].value, label : records[i].label};
}
})
.catch(function(err){
throw err;
});

db.queryAsync("SELECT * FROM safeplusdb.listmsd;")
.then(function (records){
for(i=0; i<records.length; i++)
{
let aaa = records[i].namelist;
let ccc = [];
struct.msdList[i] = {id: records[i].idlistmsd, labelList: aaa, msds : []};
}

let k = struct.msdList.length;

db.queryAsync("SELECT * FROM safeplusdb.msd;")
.then(function (records1)
{
for(j=0; j<records1.length; j++)
{
for(l=0;l<k;l++)
{
if(records1[j].listmsd==struct.msdList[l].id)
{
struct.msdList[l].msds.push(records1[j].msd);
}
}
}
})
.catch(function(err){
throw err;
});
})
.catch(function(err){
throw err;
});


}

但是,我想做出一个像这样的 super promise (为此我使用 bluebird.js)( http://bluebirdjs.com/docs/api/new-promise.html ),以便在执行所有 MySQL 请求后返回设置的结构。

但是传入的参数,如何组织呢?

我尝试使用 Promise.all 但它不起作用。

var pr1 = db.queryAsync('SELECT * FROM safeplusdb.vector;')
.then(function(records){
for(i=0; i<records.length; i++)
{
if(records[i].type=="ADSL")
{
struct.labelAdsl=records[i].type;
}
else if (records[i].type=="Cable")
{
struct.labelCable=records[i].type;
}
else if (records[i].type=="Fibre")
{
struct.labelFibre=records[i].type;
}
else if (records[i].type=="Others")
{
struct.labelOthers=records[i].type;
}
else if (records[i].type=="Ott")
{
struct.labelOtt=records[i].type;
}
else if (records[i].type=="Satellite")
{
struct.labelSatellite=records[i].type;
}
else if (records[i].type=="Tnt")
{
struct.labelTnt=records[i].type;
}
}
})
.catch(function(err){
throw err;
});

var pr2 = db.queryAsync("SELECT * FROM safeplusdb.data;")
.then(function (records){
for(i=0; i<records.length; i++)
{
let record= records[i];

if(record.vector_id==0)
{
factorizeSQL(UTCGap, struct.cllForecastAdsl, struct.valueReturnAdsl, record);
}

else if(record.vector_id==1)
{
factorizeSQL(UTCGap, struct.cllForecastCable, struct.valueReturnCable, record);
}

else if(record.vector_id==2)
{
factorizeSQL(UTCGap, struct.cllForecastFibre, struct.valueReturnFibre, record);
}

else if(record.vector_id==3)
{
factorizeSQL(UTCGap, struct.cllForecastOthers, struct.valueReturnOthers, record);
}

else if(record.vector_id==4)
{
factorizeSQL(UTCGap, struct.cllForecastOtt, struct.valueReturnOtt, record);
}

else if(record.vector_id==5)
{
factorizeSQL(UTCGap, struct.cllForecastSatellite, struct.valueReturnSatellite, record);
}

else if(record.vector_id==6)
{
factorizeSQL(UTCGap, struct.cllForecastTnt, struct.valueReturnTnt, record);
}
}
})
.catch(function(err){
throw err;
});

var pr3 = db.queryAsync("SELECT * FROM safeplusdb.admin;")
.then(function (records){
for(i=0; i<records.length; i++)
{
struct.adminList[i]=records[i].name;
}
})
.catch(function(err){
throw err;
});

var pr4 = db.queryAsync("SELECT * FROM safeplusdb.media;")
.then(function (records){
for(i=0; i<records.length; i++)
{
struct.mediaList[i] = { label : records[i].label, value : records[i].value};
}
})
.catch(function(err){
throw err;
});

var pr5 = db.queryAsync("SELECT * FROM safeplusdb.scroll ORDER BY orderscroll;")
.then(function (records){
for(i=0; i<records.length; i++)
{
let record= records[i];

if(record.isscrolled==false)
{
if(record.idm==null && record.vector_id !=null)
{
struct.listUnScroll[struct.indj] = {media : null, platform : null, orderscroll: null};
struct.listUnScroll[struct.indj].orderscroll = record.orderscroll;
struct.listUnScroll[struct.indj].platform = struct.listPlatforms[record.vector_id];
struct.indj++;
}
else if(record.vector_id==null && record.idm!=null)
{
struct.listUnScroll[struct.indj] = {media : null, platform : null, orderscroll: null};
struct.listUnScroll[struct.indj].orderscroll = record.orderscroll;
struct.listUnScroll[struct.indj].media = struct.listMedias[record.idm-1];
struct.indj++;
}
else
{
struct.listUnScroll[struct.indj] = {media : null, platform : null, orderscroll: null};
struct.listUnScroll[struct.indj].orderscroll = record.orderscroll;
struct.listUnScroll[struct.indj].media = struct.listMedias[record.idm-1];
struct.listUnScroll[struct.indj].platform = struct.listPlatforms[record.vector_id];
struct.indj++;
}
}

else if(record.isscrolled==true)
{
if(record.idm==null && record.vector_id !=null)
{
struct.listScroll[struct.indk] = {media : null, platform : null, orderscroll: null};
struct.listScroll[struct.indk].orderscroll = record.orderscroll;
struct.listScroll[struct.indk].platform = struct.listPlatforms[record.vector_id];
struct.indk++;
}
else if(record.vector_id==null && record.idm!=null)
{
struct.listScroll[struct.indk] = {media : null, platform : null, orderscroll: null};
struct.listScroll[struct.indk].orderscroll = record.orderscroll;
struct.listScroll[struct.indk].media = struct.listMedias[record.idm-1];
struct.indk++;
}
else
{
struct.listScroll[struct.indk] = {media : null, platform : null, orderscroll: null};
struct.listScroll[struct.indk].orderscroll = record.orderscroll;
struct.listScroll[struct.indk].media = struct.listMedias[record.idm-1];
struct.listScroll[struct.indk].platform = struct.listPlatforms[record.vector_id];
struct.indk++;
}
}
}
})
.catch(function(err){
throw err;
});

var pr6 = db.queryAsync("SELECT * FROM safeplusdb.device;")
.then(function (records){
for(i=0; i<records.length; i++)
{
struct.deviceList[i] = {value : records[i].value, label : records[i].label};
}
})
.catch(function(err){
throw err;
});

var pr7 = db.queryAsync("SELECT * FROM safeplusdb.listmsd;")
.then(function (records){
for(i=0; i<records.length; i++)
{
let aaa = records[i].namelist;
let ccc = [];
struct.msdList[i] = {id: records[i].idlistmsd, labelList: aaa, msds : []};
}

let k = struct.msdList.length;

db.queryAsync("SELECT * FROM safeplusdb.msd;")
.then(function (records1)
{
for(j=0; j<records1.length; j++)
{
for(l=0;l<k;l++)
{
if(records1[j].listmsd==struct.msdList[l].id)
{
struct.msdList[l].msds.push(records1[j].msd);
}
}
}
})
.catch(function(err){
throw err;
});
})
.catch(function(err){
throw err;
});

Promise.all(pr1, pr2, pr3, pr4, pr5, pr6, pr7)
.then(function(){
console.log(struct);
})
.catch(function(err){
throw err;
});
}

最佳答案

首先,您的 loadDatas 函数必须是异步的 - 这是在其中使用异步调用的结果。在您当前的实现中,您将无法理解所有异步调用何时完成并且您的结构已填充数据。

您应该考虑链接 Promise。看看这篇文章(忽略 Angular 的东西):http://solutionoptimist.com/2013/12/27/javascript-promise-chains-2/

关于javascript - Node.js 和 Mysql 的 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44541678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com