因调用方需要拿到一个promise返回的结果,所以setTimeout需要用promise
如过调用方只是单纯执行函数,直接使用setTimeout即可,无须封装promise
1.轮询操作
async poolPage (key) {
return new Promise(resolve => {
const tickFn = () => {
setTimeout(async () => {
try {
const data = await this.findField(key)
if (data) {
resolve(data)
} else {
tickFn()
}
} catch (e) {
resolve()
}
}, 1000)
}
tickFn()
})
},
async findField (key) {
let res = await reqApi({
method: 'POST',
url: `/storage/findFileId?uuid=${key}`,
body: null
})
res = JSON.parse(res)
if (res && res.errorMsg) {
return null
}
if (res && res.currentPage) {
return res
}
},
2.调用
const data = await this.poolPage(res)