[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: should .finalize be awaited? #772

Open
nicojs opened this issue Jul 4, 2024 · 0 comments
Open

Question: should .finalize be awaited? #772

nicojs opened this issue Jul 4, 2024 · 0 comments

Comments

@nicojs
Copy link
nicojs commented Jul 4, 2024

After upgrading eslint, I noticed that .finalize() returns a promise. I can see that this is also documented here: https://www.archiverjs.com/docs/archiver#finalize. However, it has yet to be awaited in the readme example. Can I simply await this promise instead of using the error and close event handlers?

To be clear:

This:

async function zip(from: string, to: string) {
	const output = fs.createWriteStream(to);
	const archive = archiver('zip', {
		zlib: { level: 9 }, // Sets the compression level.
	});
	archive.pipe(output);
	archive.directory(from, false);
	await archive.finalize();
	console.log(`✅ ${to} (${archive.pointer()} bytes)`);
}

Instead of this:

function zip(from: string, to: string) {
	return new Promise<void>((res, rej) => {
		const output = fs.createWriteStream(to);
		const archive = archiver('zip', {
			zlib: { level: 9 }, // Sets the compression level.
		});
		output.on('close', () => {
			console.log(`✅ ${to} (${archive.pointer()} bytes)`);
			res();
		});
		archive.on('error', err => rej(err));
		archive.pipe(output);
		archive.directory(from, false);
		archive.finalize();
      }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant