Function not returning value back after call to wixData.query . Any help appreciated

// Function to return the number of items in my collection which is 699//
// However it returns the promise from the wixData.query what am i doing wrong ?// // I realise I am doing something stupid but cannot figuer it out. I would like to know why not just the solution if possible.// // If someone would be kind enough to help please , I would be very grateful.//
// Its something I think with the call to wixData.query function , I may well be wrong.//
//Thank you for taking time help a very frustrated me, please don’t laugh to much I am a beginner) //

import wixData from ‘wix-data’ ;

var count =fetchNumberOfRecords(count) // I expected numberOfItems 699 retured. //

console.log( "After function call = " ,count); // It returns 'Promise<> ’ ? Not 699 //

function fetchNumberOfRecords (param){

return wixData.query( “Contacts2” ,count)
.count()
.then( (num) => {
let numberOfItems = num;

console.log( "numberOfItems fund value = " ,numberOfItems); // Value correct 699//

return (numberOfItems); // I expected this to return value 699//

} )
. catch ( (error) => { let errorMsg = error.message;
let code = error.code;
} );

}

That’s because fetchNumberOfRecords is a promise and you should wait for its resolution before you can log it to the console.
something like:

fetchNumberOfRecords(count)
.then(res => console.log("After function call = ", res));

Once again thank you for the help .That completely makes sense I should of realised, however not sure I would of though. Its very reassuring to get your help I would gladly pay for such excellent advice I would be lost without it. Once again I can continue after getting stuck. I cannot thank you enough. What is clearly simple for you if mind bending for me , I tried for ages to figure out what was going on. Thank you Coz.

You’re welcome. I’m glad it worked for you :slight_smile:

@jonatandor35 I have got it working fine from your help. However, I have experimented since you kindly pointed me in right direction. Could I just ask can I use ‘await’ with my function type? l I have tried various ways but cannot get it to except the command at all. Thought I would ask in case you cannot. Also is waiting for a task to end deemed an old procedural method of code and therefore not good practice to use ‘await’. I won’t keep asking you things I appreciate your busy no doubt. Thank you. Coz.

@coz You can always use await instead of promise.then()
I don’t know exactly what you’re trying to do, but you can, for example, instead of:

fetchNumberOfRecords(count)
.then(res => console.log("After function call = ", res));

Do something like:

(async function() {
const numOfrecords = await fetchNumberOfRecords(count);
console.log("After function call = ", numOfrecords)
}())

@jonatandor35 That works thank you. I found the initial answer you gave me underlined a powerful feature within the scripting I wanted to investigate this, so I understood it a little more.

I am developing a library for myself to learn of functions to use in my back office dashboard. This function was to count the records in my collections which I could call and display. I was curious about the ‘async’ and ‘wait’ after your initial answer to my problem made me investigate more widely the best way to use your first response. it as was most interesting. I am from 8086 assembler era (way before your time I expect) scripting language is very different prospect, reducing clock cycles was important per task, waiting around was never a luxury I had. Your help is invaluable to me and always clear which I am very grateful for. No doubt I will have other problems I may ned to post along my journey. I sincerely thank you. Coz

You’re welcome. :blush: Feel free to ask and discuss any subject.

@jonatandor35 Back again. I have posted another (no doubt) issue I have if you get a chance could you take a look that would be great as I am stuck again. Not sure if this is the best way to contact you apologies if not.
Thank you I appreciate any help. Coz