Skip to content

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

Description

@NanaVan

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();

Activity

  1. lovasoa commented on Nov 15, 2023

    @lovasoa
    Member

    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.

  2. NanaVan commented on Nov 16, 2023

    @NanaVan
    Author

    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.

  3. lovasoa commented on Nov 16, 2023

    @lovasoa
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions