enhance checkAndDownloadDependenciesForInit - adjust promise logic

This commit is contained in:
geekgeekrun
2024-02-25 09:11:45 +08:00
parent 45bf49c1ba
commit b115f0fbc3
@@ -26,15 +26,27 @@ export const checkAndDownloadDependenciesForInit = async () => {
try { try {
let timeoutTimer = 0 let timeoutTimer = 0
await new Promise((resolve, reject) => { const promiseWithResolver = (() => {
const o = {} as unknown as {
promise: Promise<unknown>
resolve: (result: unknown) => void
reject: (reason: unknown) => void
}
o.promise = new Promise((resolve, reject) => {
o.resolve = resolve
o.reject = reject
})
return o
})()
checkAndDownloadPuppeteerExecutable({ checkAndDownloadPuppeteerExecutable({
downloadProgressCallback(downloadedBytes: number, totalBytes: number) { downloadProgressCallback(downloadedBytes: number, totalBytes: number) {
clearTimeout(timeoutTimer) clearTimeout(timeoutTimer)
if (downloadedBytes !== totalBytes) { if (downloadedBytes !== totalBytes) {
timeoutTimer = setTimeout(() => { timeoutTimer = setTimeout(() => {
// will encounter this when network disconnected when downloading // will encounter this when network disconnected when downloading
reject(new Error('PROGRESS_NOT_CHANGED_TOO_LONG')) promiseWithResolver.reject(new Error('PROGRESS_NOT_CHANGED_TOO_LONG'))
}, 30 * 1000) }, 5 * 1000)
} }
console.log(downloadedBytes / totalBytes) console.log(downloadedBytes / totalBytes)
pipeWriteRegardlessError( pipeWriteRegardlessError(
@@ -46,17 +58,18 @@ export const checkAndDownloadDependenciesForInit = async () => {
}) })
) + '\r\n' ) + '\r\n'
} }
}).then(
() => {
resolve(void 0)
},
(err) => {
reject(err)
}
)
}) })
.then(() => {
promiseWithResolver.resolve(void 0)
})
.catch((err) => {
promiseWithResolver.reject(err)
})
await promiseWithResolver.promise
app.exit(DOWNLOAD_ERROR_EXIT_CODE.NO_ERROR) app.exit(DOWNLOAD_ERROR_EXIT_CODE.NO_ERROR)
} catch (err) { } catch (err) {
console.error(err)
app.exit(DOWNLOAD_ERROR_EXIT_CODE.DOWNLOAD_ERROR) app.exit(DOWNLOAD_ERROR_EXIT_CODE.DOWNLOAD_ERROR)
} }
} }