Skip to content Skip to sidebar Skip to footer

Create Knex Querybuilder And Execute Later

I'm hoping to create a queryBuilder object, and defer execution until I'm ready. The following code allows me to pass a queryBuilder object between functions without executing the

Solution 1:

You want to 'defer execution until' you are 'ready.'

It will depend on what you mean by 'ready', but you can fire the query when the desired action is finished using Promise.

For example, if you want to execute the query after 5 seconds delay, using the example from MDN Promise doc, you can do something like this:

let myFirstPromise = newPromise((resolve, reject) => {
  // We call resolve(...) when what we were doing asynchronously was successful, and reject(...) when it failed.// In this example, we use setTimeout(...) to simulate async code.// In reality, you will probably be using something like XHR or an HTML5 API.setTimeout(function(){
    resolve("After this value is resolved with 5000ms delay, "); // Yay! Everything went well!
  }, 5000);
});

myFirstPromise.then((successMessage) => {
  // successMessage is whatever we passed in the resolve(...) function above.// It doesn't have to be a string, but if it is only a succeed message, it probably will be.// You can fire your query hereconsole.log(successMessage + "you can execute your query inside this then(..) function!");
  // execute(getBaseSelect());
});

If you are waiting on multiple promises, you can use Promise.all(..) like this:

Promise.all([myFirstPromise, mySecondPromise, myThirdPromise])
  .then((values) => {
    return execute(values[0], values[1], values[2])
  });

Post a Comment for "Create Knex Querybuilder And Execute Later"