How to export JavaScript object that has Promise functions?

I have a module that handles database pool creation. I created an export function to expose database pool for other fivem modules.

exports('getPool', () => {
    return pool;
});

Now I can access this pool from other resources, however this object has a method getConnection(), which returns connection via a JavaScript Promise API. When I call this method an empty object is returned to me in code.

const dbPool = exports["my-resource"].getPool(); // Object.keys(dbPool) has all functions
const conn = await dbPool.getConnection(); // <-- will be completely unrelated object where Object.keys(conn) === []

Of course it works when I will duplicate pool code in this resource, but this will cause a problem of having multiple database pools in runtime.

Maybe somebody has encountered similar problem and can help me.

2 Likes

Not supported at this time. You could provide a PR that implements __cfx_async_retval support similar to C# and Lua have already.

(update in 2022 as people still find this post: implemented fine as of this PR feat(scripting/v8): Add support for __cfx_async_retval by dunak-debug ¡ Pull Request #975 ¡ citizenfx/fivem ¡ GitHub)

1 Like

Can you link me to a GitHub project, so I could take a look? Thanks.

2 Likes

How to build Linux artifacts out of fork?

I did look at gitlab-ci and “build_server_proot.sh” script, but there are some env variables like FIVEM_PRIVATE_URI, which I don’t seem to find any documentation on. Plan was to quickly spin up CI process and start testing the changes.

Unless, there is a better way to do it, which I haven’t found.

Thanks!

1 Like

Just check what the .travis.yml does.

2 Likes

There’s a PR here, that’s currently a draft… Any info on what’s missing in this to have it worth a merge?

No idea it’s not my pull request. I forked the repo in 2020 and tried modifying the engine (through trial & error) to support async & Promise, but I never got it working. My fork can be found here Commits · bound2/fivem · GitHub