1

I am writing javascript first time. within the azure function app I have one async function to insert data using query see function code as below

async function insertData(query) {

var connection = new Connection(config);
var result = 'Connecting'

await connection.on('connect', function (err) {
    if (err) {
        console.log('Connect: ' + err);
        result = 'Connect: ' + err;
    } else {
        request = new Request(query + "; select @@identity", function (err, rowCount) {
            if (err) {
                console.log('Insert: ' + err);
                result = 'Insert: ' + err;
            } else {
                console.log('Insert complete.');
                result = 'Insert complete';
            }
            connection.close();
        });
        connection.execSql(request);
    }
});
return result;
};

and I am calling this fuction from main triggerd function like this

   var result = insertData(query);  // calling function from here
   context.log('Result ' + result); // Result [object Promise] 

but it is not giving proper value of result at log it is Result [object Promise] it should be Insert complete as it inserted recorted successfuly can you please currect me? Thanks!

Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113
  • Javascript actually works in asynchronous way, it will send a request and returns a promise. It will then automatically move to your next code to execute. To resolve this you need to use the promise with then. use the following code result.then(function(result){ //Your Success code }, function(error) {alert("error"}); – Joe 89 Jan 24 '19 at 08:34

0 Answers0