function getMusicName() {
db()
.then(function(connection) {
var sql = 'select name from musicInfo';
return queryMusicInfo(connection,sql);
},fail)
.then(function(res){
console.log(res)
})
}
因为then中很多函数都是异步的。
有没有什么办法在getMusicName这个函数中获取最后这个then返回的结果呢?还是promise有没有什么机制能够解决这种问题呢?
打个比喻:
你在2016年去银行查看自己的账户有多少钱,但银行的计算系统非常慢,要2017年才能查出来。你不想等,于是银行向你保证,查出结果会主动通知你。
然后你现在的问题是,能不能在2017年查出结果的时候请银行告诉2016年的自己。
答案很明显,不能。
——————————————————
哦呵呵,我好像误会了你的意思。可以把第二个then去掉,然后return这个promise给外面的函数。