index.ts 788 B

12345678910111213141516171819202122232425262728
  1. export async function sleep(time: number) {
  2. await new Promise((resolve) => {
  3. setTimeout(() => resolve(null), time);
  4. });
  5. }
  6. export function downloadAsFile(filename: string, content: string) {
  7. const file = new Blob([content], {
  8. type: "text/plain",
  9. });
  10. const fileURL = URL.createObjectURL(file);
  11. downloadUsingAnchor(fileURL, filename);
  12. }
  13. export function downloadUsingAnchor(link: string, name: string) {
  14. const a = document.createElement("a");
  15. a.style.display = "none";
  16. a.href = link;
  17. a.download = name;
  18. document.body.appendChild(a);
  19. a.click();
  20. URL.revokeObjectURL(link);
  21. a.remove();
  22. }
  23. export function isPromise<T>(obj: T | Promise<T>): obj is Promise<T> {
  24. return obj && typeof (obj as any).then === "function";
  25. }