len = infodata.length; if (len < 5) { for (var i = 0; i < len; i++) { } } else { var html,ids=[]; for (var i = 0; i < 5; i++) { var l = parseInt(Math.random() * 10); ids.push(infodata[l].id) } //需要用每一个id去查询返回的不同的数据,最后进行统一处理 getAllResults(ids, function(res){ console.log(res) }) } function getAllResults(groupIds, callBack) { let allResults = []
//对多个promise进行统一处理,返回结果 Promise.all(getPromiseArray(groupIds)).then(function (values) { for (let i = 0; i < values.length; i++) { if(values[i].data){ allResults.push(values[i].data) } else { allResults.push(values[i]) } } callBack(allResults) }) }
// 返回多个成功的promise对象 function getPromiseArray(groupIds){ var promiseArray = []; videoArr = []; for (var i = 0; i < groupIds.length; i++) { let promise = play(groupIds[i]); promiseArray.push(promise) } return promiseArray; } function renderdata(infodata) { l = parseInt(Math.random() * 10); return new Promise(function (resolve, reject) { return play(infodata[l].id).then(function (res) { return resolve(res) }) }) } function play(id){ var params={ cameraid:id } return new Promise(function(resolve,reject){ $s.get('artemispost/getCameraPreviewURL_S.do', params, function (res) { resolve('http://'+Math.random()*10) }, true,true) })