electron / asar

Simple extensive tar-like archive format with indexing
MIT License
2.57k stars 248 forks source link

Any way to synchronously create a package programatically? #215

Closed GNUGradyn closed 1 year ago

GNUGradyn commented 3 years ago

Hi! I noticed asar.createPackage is a couroutine. I need to synchronously create a package programatically?

sploders101 commented 3 years ago

I believe this was intentional. This library used to be completely synchronous (which is why I made asar-async), but it was very frustrating because your entire program would have to stop until the package was finished, which took up way more time than it needed to. I'd suggest you look into async functions and promises, since that will provide a similar programming style to synchronous calls.

Here's some good documentation on it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Typically, when I'm writing a one-off script and need something to be synchronous when it's not, I'll use an IIFE (immediately-invoking function expression). Here's what it looks like:

(async () => {
    // Your code goes here
    const myThing = await anAsyncFunction();
    console.log(myThing);
})();