Can someone who actually knows what they are doing help me with some.js

I am busy trying to use knex.js within custom modules.

knex will well documented and not prosing too much problem EXCEPT i have never worked with promises before, not really a .js programmer (yet, working hard on it as i never use a any of the others i learned over the years)

so as a simple example

const db = this.getDbConnection(connection);
  if (!db) throw new Error(`Connection "${connection}" doesn't exist.`);

sqlCode="select username from users where userid = 1";
let results = await db.raw(sqlCode);
        // raw method returns raw response from driver, needs some conversion (http://knexjs.org/#Raw-Queries)
        if (db.client.config.client == 'mysql' || db.client.config.client == 'mysql2') {
          results = results[0];
        } else if (db.client.config.client == 'postgres' || db.client.config.client == 'redshift') {
          results = results.row;
        }
 console.log(results); // correctly outputs the content of results
        return results;

but the code returns an object as a promise

i can view the returned data with

 console.log(results)

[ { username: 'Brian' } ]

using "typeof" datatype is shown as : Promise { 'object' }

What do i need to do to resolve the promise and then use the returned data in subsequent operations.

thanks in advance

Try to use results.username

Sadly that returns "undefined"

got it, it's an array

results[0].username

Thanks for pointing me in the right direction @Notum

1 Like

Looks like a :bulb: moment to me :wink:

1 Like

1 Like

Hmm, not quite solved,

While the value can be obtained as above, that does not mean that the promise has been resolved so sometimes when trying to address the value, the data is not available and i see

value is: [object Promise]

this seems to follow any subsequent knex() call (the module i am writing makes data queries in a loop so they happen quickly)
Guessing i need some code to check that the promise has been returned.

Time to stop coding project for a while and start reading all about promises i guess.

Anyone kow any good references, appreciated

If you use "await", the Promise is guaranteed to be resolved:

let results = await db.raw(sqlCode);

The code will not move to the next line until the Promise is resolved.

Are you missing an await somewhere?

Also, you can search posts of mine mentioning "await" or "promises"

1 Like

The call does have await.

The function is called with:

 transresults = getTrans(uuidValue, language_code, DataFieldsArray[offset], toTranslatePos);

the function tis defined with

      async function getTrans(targetUUID, lcode, searchField, replacePositon) {

I am guesting i need to add a

.then

into this somewhere to receive the promise when returned?

I don't see any await here :laughing:

Should be:
transresults = await getTrans(...)

Using .then is the old-school way, you should only use async and await nowadays

Sorry Apple, excuse my stupidity, done so many variations my brain is fried

var transresults = await getTrans(uuidValue, language_code, DataFieldsArray[offset], toTranslatePos);

seems to do the trick, i was putting the await in wrong place

await transresults =  getTrans(...

which caused errors so i took it out

Yes, thats me 66 and getting long in the tooth with coding also.

But yes, skills do need tweaking which is why am am doing all the extensions, helps me tweak and test my techniques.
Thanks again for your help.

1 Like

I remember when I was first learning about Promises. Took me quite a fair amount of time to learn. In fact, I despised it so much I went straight for the experimental async/await. Only months later when I had to "promisify" a callback style function is that it clicked for me :slight_smile:

By the way, a callback style function is older than promises.

The goal of async/await is you can write asynchronous code in a synchronous manner.

The next evolution is outside Javascript, it's in the Go programming language, it's wonderful for building async stuff.

1 Like

Yes, promises and ES6 arrow functions (never used them) are on my current learn list. Found some really good tutorial videos

1 Like