Giter Club home page Giter Club logo

Comments (9)

kylefarris avatar kylefarris commented on June 20, 2024

Good catch on the bad example. I would happily accept a PR to fix it.

As for a Promise API, I definitely want this to happen as well. Callback hell should be a think of the past. I, for one, welcome our new async/await overlords. I think I set Promise support for v3. For now, you can get away with it (mostly, I think) by "promisifying" any method using Node's util.promisify() method.

from node-querybuilder.

Flamenco avatar Flamenco commented on June 20, 2024

IIRC promisify was not working in my use case, at least with V1.

from node-querybuilder.

kylefarris avatar kylefarris commented on June 20, 2024

Have you tried with the v2 branch. Also, looks like I haven't actually made v2 the master yet. Have you been using the v2 branch?

from node-querybuilder.

Flamenco avatar Flamenco commented on June 20, 2024

Yes. All in with V2. Except my use case is webpacking everything into a single script, and then running inside embedded V8 runtime inside Java (with promise but not async/await support), and that running as a WebApp inside Tomcat. Not really typical...

from node-querybuilder.

kylefarris avatar kylefarris commented on June 20, 2024

Haha, yeah, I would say that is pretty atypical indeed. Okay then, I'll go ahead and make v2 the master branch so you don't have to keep using /next.

from node-querybuilder.

kylefarris avatar kylefarris commented on June 20, 2024

If you want to tackle making the API support promises (without breaking the CB API), I would be very please to merge that PR. Definitely try the util.promisify first, though, as a stop-gap solution and lemme know if that works at all.

from node-querybuilder.

Flamenco avatar Flamenco commented on June 20, 2024

I have actually added some promise support and am using a hacked branch...

from node-querybuilder.

Flamenco avatar Flamenco commented on June 20, 2024

@kylefarris I moved some of my code to a pure node environment and promisify (from util) did not work. It's also a PITA to first get async pool db and then promisify, as getting the db from pool should also return a promise IMO. So first order of business should be to return a promise from pool.getDb()...

I will start a new branch forked off of v2, and will not add support to v1. V2 seems stable enough for my uses.

This seems to be how it's done

        let promise;

        if (!callback && typeof Promise === 'function') {
            promise = new Promise((resolve, reject) => {
                callback = shared.callbackPromise(resolve, reject);
            });
        }
        // ...
       return promise

Sorry to pollute this thread...

from node-querybuilder.

kylefarris avatar kylefarris commented on June 20, 2024

I don't think it's polluting the thread at all. We have to have this conversation somewhere.

I've written several project where I've had to introduce Promises into a callback-centric API. It's actually easier than you might think.

There's really only a few methods (the main execution method and the connection methods) we'd need to change in order to add the Promise functionality. Without testing this theory, there's one place we'd actually have to add it since all the exec methods ultimately call the _exec method on their driver's query_exec class. So, it'd just be a matter of returning a Promise if not callback is provided on that one.

As for the connection methods, we just need to worry about the get_connection() method for pools and the connect() method for single connections. We'd also have to add it to the disconnect() method for all adapter types.

I'll see if I can come up with an example. Overall, it should be a pretty small overall change. Maybe even small enough to make it a v2.1 (as long as we don't break anything).

from node-querybuilder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.