From 6de68ce1d29a66f8031fbcd65ac9f280debe6cf1 Mon Sep 17 00:00:00 2001 From: weizman Date: Tue, 7 Mar 2023 03:18:57 +0200 Subject: [PATCH] use promise instead of cb --- ui/helpers/utils/export-utils.js | 37 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/ui/helpers/utils/export-utils.js b/ui/helpers/utils/export-utils.js index 5e598e6b7a6a..8479237d0bc4 100644 --- a/ui/helpers/utils/export-utils.js +++ b/ui/helpers/utils/export-utils.js @@ -1,23 +1,24 @@ import { getRandomFileName } from './util'; -function encodeB64(text, cb) { - const buf = new TextEncoder().encode(text); - // eslint-disable-next-line no-undef - const reader = new FileReader(); - reader.onload = () => cb(reader.result.split(',')[1]); - // eslint-disable-next-line no-undef - reader.readAsDataURL(new Blob([buf])); +function encodeB64(text) { + return new Promise((resolve) => { + const buf = new TextEncoder().encode(text); + // eslint-disable-next-line no-undef + const reader = new FileReader(); + reader.onload = () => resolve(reader.result.split(',')[1]); + // eslint-disable-next-line no-undef + reader.readAsDataURL(new Blob([buf])); + }); } -export function exportAsFile(filename, data, type = 'text/csv') { - encodeB64(data, (b64) => { - // eslint-disable-next-line no-param-reassign - filename = filename || getRandomFileName(); - const elem = window.document.createElement('a'); - elem.href = `data:${type};Base64,${b64}`; - elem.download = filename; - document.body.appendChild(elem); - elem.click(); - document.body.removeChild(elem); - }); +export async function exportAsFile(filename, data, type = 'text/csv') { + const b64 = await encodeB64(data); + // eslint-disable-next-line no-param-reassign + filename = filename || getRandomFileName(); + const elem = window.document.createElement('a'); + elem.href = `data:${type};Base64,${b64}`; + elem.download = filename; + document.body.appendChild(elem); + elem.click(); + document.body.removeChild(elem); }