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 about async encodeParameters in extension #2520

Open
exquisitus opened this issue Apr 29, 2024 · 0 comments
Open

Question about async encodeParameters in extension #2520

exquisitus opened this issue Apr 29, 2024 · 0 comments

Comments

@exquisitus
Copy link

Hello, I have defined an extension, which tries to read some data from IndexedDB, process it and submit it to an HTTP endpoint.

I have problems because Dexie.js works asynchronously, and I cannot make encodeParameters() wait for the result of the db.notes.where() call, which is a Promise.

So, the payload of my HTTP Request is '[object Promise]', while I would have wanted it to be the result of the eventually fulfilled Promise, i.e. something like
3 100324 104773 108681
6 12309248 89069683
(which is what is printed in the console by the statement console.log(ret)

Is it possible in principle, to make encodeParameters wait for the Promises to be fulfilled before returning? I post one of my many attempts, where I have made the function async and use an await statement.

htmx.defineExtension('notes-plain-h', {
    onEvent: function (name, evt) {
        if (name === "htmx:configRequest") {
            evt.detail.headers['Content-Type'] = "text/plain";
        }
    },

    encodeParameters : async function(xhr, parameters, elt) {
        xhr.overrideMimeType('text/plain');
        let dict = {};
        let result = await db.notes.where('suit').equals('h').each(n => {
            if (!(n.marketId in dict))
              dict[n.marketId] = [];
            dict[n.marketId].push(n.productId);
        })
        .then(() => { return dict; })
        .then((d) => {
          console.log(d);
          let ret = ""
          for (const [m_id, products] of Object.entries(d)) {
            let s = m_id;
            for (let p_id of products)
              s = s + " " + p_id;
            ret = ret + s + "\n";
          };
          console.log(ret);
          return(ret);
        })
        return result;
    }
});

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