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

An update requirement for using fetch loading a database from a server #557

Closed
NanaVan opened this issue Nov 14, 2023 · 3 comments
Closed

Comments

@NanaVan
Copy link

NanaVan commented Nov 14, 2023

The original example for Promise.all and await can not work functionally together (see MDN for details.).
A new example can be adopted as:

async function fetchSQL(){
    const [SQL, buf] = await Promise.all([
        initSqlJs({locateFile: file => `https://path/to/your/dist/folder/dist/${file}`}), 
        fetch("/path/to/database.sqlite").then(res => res.arrayBuffer())
    ]);
    const db = new SQL.Database(new Uint8Array(buf));
    // other command as below
}
fetchSQL();
@lovasoa
Copy link
Member

lovasoa commented Nov 15, 2023

This is not a valid bug report. Please give details about what you think the problem is. I don't see an issue with the example code you gave.

@lovasoa lovasoa closed this as completed Nov 15, 2023
@NanaVan
Copy link
Author

NanaVan commented Nov 16, 2023

The code block I gave is the corrected one.
The original example:

const sqlPromise = initSqlJs({
  locateFile: file => `https://path/to/your/dist/folder/dist/${file}`
});
const dataPromise = fetch("/path/to/database.sqlite").then(res => res.arrayBuffer());
const [SQL, buf] = await Promise.all([sqlPromise, dataPromise])
const db = new SQL.Database(new Uint8Array(buf));

will raise an error as SyntaxError: await is only valid in async functions, async generators and modules as shown in MDN.

That's why I recomment to update the example for fetch in browser.

@lovasoa
Copy link
Member

lovasoa commented Nov 16, 2023

Oh, I get what you are saying. It seemed evident to me that the given example had to be run in an async context (which is now supported at the root level in many places, including node). If you want to add a note to specify that (without changing the code snippet), I'll accept a pull request :)

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

2 participants