export async function reStartJobTask(params?: Record<string, any>, options?: Record<string, any>) { return request('/api/platform/v1/job/recreate', { method: 'POST', data: params, ...(options || {}), }); }
时间: 2024-02-23 12:56:53 浏览: 97
这是一个使用 TypeScript 语言编写的异步函数,其名称为 `reStartJobTask`,接受两个可选参数 `params` 和 `options`。该函数使用 `request` 方法向 `/api/platform/v1/job/recreate` 发送一个 POST 请求,传递 `params` 作为请求体,并将 `options` 参数合并到请求中。该函数返回一个 Promise 对象。
相关问题
优化这段代码:import { reactive, toRaw } from 'vue' // 分页钩子函数 interface Options { page?: number size?: number fetchFun: (_arg: any) => Promise<any> params?: Record<any, any> firstLoading?: boolean } export function usePaging(options: Options) { const { page = 1, size = 15, fetchFun, params = {}, firstLoading = false } = options // 记录分页初始参数 const paramsInit: Record<any, any> = Object.assign({}, toRaw(params)) // 分页数据 const pager = reactive({ page, size, loading: firstLoading, count: 0, lists: [] as any[], extend: {} as Record<string, any> }) // 请求分页接口 const getLists = () => { pager.loading = true return fetchFun({ page_no: pager.page, page_size: pager.size, ...params }) .then((res: any) => { pager.count = res?.count pager.lists = res?.lists pager.extend = res?.extend return Promise.resolve(res) }) .catch((err: any) => { return Promise.reject(err) }) .finally(() => { pager.loading = false }) } // 重置为第一页 const resetPage = () => { pager.page = 1 getLists() } // 重置参数 const resetParams = () => { Object.keys(paramsInit).forEach((item) => { params[item] = paramsInit[item] }) getLists() } return { pager, getLists, resetParams, resetPage } }
以下为优化后的代码:
import { reactive, toRaw } from 'vue'
interface Options {
page?: number
size?: number
fetchFun: (_arg: any) => Promise<any>
params?: Record<any, any>
firstLoading?: boolean
}
export function usePaging(options: Options) {
const { page = 1, size = 15, fetchFun, params = {}, firstLoading = false } = options
const paramsInit: Record<any, any> = toRaw(params)
const pager = reactive({
page,
size,
loading: firstLoading,
count: 0,
lists: [] as any[],
extend: {} as Record<string, any>
})
const getLists = async () => {
pager.loading = true
try {
const res = await fetchFun({
page_no: pager.page,
page_size: pager.size,
...params
})
pager.count = res?.count
pager.lists = res?.lists
pager.extend = res?.extend
return Promise.resolve(res)
} catch (err) {
return Promise.reject(err)
} finally {
pager.loading = false
}
}
const resetPage = () => {
pager.page = 1
getLists()
}
const resetParams = () => {
Object.assign(params, paramsInit)
getLists()
}
return {
pager,
getLists,
resetParams,
resetPage
}
}
优化的部分包括:
1. 使用 async/await 替代 Promise.then() 语法,使代码更加简洁易读,同时也更具可读性。
2. 重置参数部分使用 Object.assign() 替代 Object.keys(paramsInit).forEach() 语法,可以更简洁地实现参数重置操作。
阅读全文