jsDoc 辅助vsocde函数参数提示

jsDoc 辅助函数参数提示

/**
 * 模拟请求的异步函数
 * ```javascript
 * // 省略时间传值
 * sleep({ data: '返回值', code: 200 }).then(res => console.log(res))
 * // 也可以自己加时间
 * sleep({ data: '返回值', code: 200 }, 2000).then(res => console.log(res))
 * ```
 * @template R
 * @param {R} args 模拟请求返回的参数
 * @param {number} [time_ms=1000] 模拟请求消耗的时间毫秒 默认 1000ms
 * @return {Promise<R>} 返回值
 */
export const sleep = (args, time_ms = 1000) => new Promise((resolve) => setTimeout(resolve, time_ms, args))

/**
 * 模拟请求的异步函数
 * ```javascript
 * // 省略时间传值
 * sleep({ msg: 'success' }, { msg: 'error' }).then(res => console.log(res))
 * // 也可以自己加时间
 * sleep({ msg: 'success' }, { msg: 'error' }, 500).then(res => console.log(res))
 * ```
 * @template R
 * @template J
 * @param {R} resolveData 模拟请求成功返回的参数
 * @param {J} rejectData 模拟请求失败返回的参数
 * @param {number} [time_ms=1000] 模拟请求消耗的时间毫秒 默认 1000ms
 * @return {Promise<R>} 返回值
 */
export const sleep = (
  resolveData,
  rejectData,
  time_ms = 1000,
  random = Math.floor(Math.random() * 2), // 随机 1,0 模拟请求成功失败
) => new Promise((resolve, reject) =>
  setTimeout(random ? resolve : reject, time_ms, random ? resolveData : rejectData))

sleep({ msg: 'success' }, { msg: 'error' })
  .then(res => console.log(res))
  .catch(res => { console.log(res) })
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值