new Promise用法
New Promise用于在一个方法(a)中调用另外一个带有ajax的方法(b)并且a方法中需要b方法中ajax返回的数据,此时可以使用new Promise 例如:
//带有ajax的b方法
getPmmlFilePath(){
if (this.nodeId){
return new Promise((resolve, reject) => {
this.queryStatFilepathByNodeId({ nodeId: this.nodeId }).then(res => {
if (res.success){
this.filePathObj = res.data;
resolve(res.data)
}
});
})
}
},
// 方法a
downloadFilePath(){
this.getPmmlFilePath().then(res =>{
if (res && res.filePath){
let filepath = this.filePathObj.filePath;
let lastIndex = filepath.lastIndexOf('/');
let filename = filepath.substring(lastIndex + 1);
if (filename){
let aDom = document.createElement('a');
aDom.download = filename;
aDom.href = filepath;
aDom.style.display = 'none';
document.body.appendChild(aDom);
aDom.click();
document.body.removeChild(aDom);
}
}
});
},
如例所示:downloadFilePath方法需要用到getPmmlFilePath方法所返回的数据,此时就可以使用return new Promise
定时操作:
setInterval(()=>{
//代码块
},间隔时间)
延时操作:
setTimeout(() =>{
//要延迟的代码
},延迟时间);