/**
 * 封装 Promise，不会抛异常，resolve 的时候返回 [data, null], reject 的时候返回 [null, error]
 * @param promise
 * @return resolve 的时候返回 [data, null], reject 的时候返回 [null, error]
 */
export async function to<T, U = any>(promise: Promise<T>): Promise<[T, null] | [null, U]> {
  try {
    const data = await promise;
    return [data, null];
  } catch (error) {
    return [null, error as U];
  }
}

export default to;

/**
 * 休眠
 * @param ms - The number of milliseconds to sleep.
 * @return A promise that resolves after the specified amount of time.
 */
export function sleep(ms: number) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, ms);
  });
}
