how to get a web module function to return in time for a result of a function?

Hi,

The above question may not sound how I am working at the moment,

so I have a JSW module in backend files with the following function:

const EC = require('elliptic').ec;
import { v1 as uuidv1 } from 'uuid';
const ec = new EC('secp256k1');
import wixData from 'wix-data';
import wixUsers from 'wix-users-backend';

export function sign(dataHash) {
    console.log("sign called");
    console.log(dataHash);
    wixData.query("userWallets", wixUsers.currentUser.id)
        .find()
        .then((results) => {
 if (results.items.length > 0) {
 let wallet = results.items[0];
 const privateKey = wallet.privateKey;
 var signature = ec.keyFromPrivate(privateKey).sign(dataHash);
                console.log(signature);
 return signature;
            }
        })
}

my aim is to return the signature to a function for transactions this I provide below:

static signTransaction(transaction, senderWallet) {
        console.log("sign transaction called");
 var dataHash = ChainUtil.hash(transaction.outputs);
 var signature = sign(dataHash);
        transaction.input = {
            timestamp: Date.now(),
            amount: senderWallet.balance,
            address: senderWallet.publicKey,
            signature: signature
        }
    }

at present the sign transaction function is called and responded quicker than the function on the backend JSW file is returned so when I console.log the transaction I get the following:


update or add transaction called transaction-pool.js Line 9

0 transaction.js Line 50

049ee9f357d2f742479136dc50cfcbfbbb04efd39ff15c32800bb69cb0f96724361cfbb8ea91e9ee5d97dcc2ae92a83fe7819b771a8bbc9be310e5ee289127e050 transaction.jsLine 51

1 transaction.js Line 52

Test transaction.js Line 53

sign transaction called transaction.js Line 86

hash Called chain-util.js Line 13

3eed20fcb602a3618320e8c3a7f5112f731b5f4ebc82980a102d13f18d047dc5 chain-util.js Line 14
{…}
id:
“b65e13a0-d8f4-11ea-91b4-07b1f2c45b16”
input:
{…}
timestamp: 1596835614070
amount: 1
address:
“049ee9f357d2f742479136dc50cfcbfbbb04efd39ff15c32800bb69cb0f96724361cfbb8ea91e9ee5d97dcc2ae92a83fe7819b771a8bbc9be310e5ee289127e050”
signature:
{…}. //////////////////////////// This is where the signature should be??////////////////////////
outputs:
Array(2)
0:
{…}
amount:
0
address:
“049ee9f357d2f742479136dc50cfcbfbbb04efd39ff15c32800bb69cb0f96724361cfbb8ea91e9ee5d97dcc2ae92a83fe7819b771a8bbc9be310e5ee289127e050”
1:
{…}
amount:
“1”
address:
“Test”
transaction.js Line 56

sign called walletSignature.jsw Line 9

3eed20fcb602a3618320e8c3a7f5112f731b5f4ebc82980a102d13f18d047dc5

walletSignature.jsw Line 10 ///////////// this is where the signature is returned??? /////////////////
{…}
r:
“cfedf6d41f3b0ace3f255d7b3987d2588c9a5c0acb24c3ae9ac5d3a05db1fdf5”
s:
“b3ac2712098b7486e05bacd1974b7c88b4509499ddcae9a9eca8b221575540e0”
recoveryParam:
1


Any help and assistance in getting the signature to return in the transaction would be amazing.

Best wishes,

Si