0

I have the following in Typescript:

import sql = require("mssql");
const config: sql.config = {....
}

const connect = async() => {
    return new Promise((resolve, reject) => {
        new sql.ConnectionPool(config).connect((err) => {
            if (err) {
                reject(err);
            } else {
                console.log("CONNECTED");
                resolve();
            }
        });
    });
};

(async() => {
    await connect().then(
        () => {
            console.log("Connection pool created successfully.");
        }).catch((err) => {
        console.error(err);
    });
})();

console.log("Now proceeding to load...");

I always get the console output in the following order:

Now proceeding to load...
CONNECTED
Connection pool created successfully

What have I done wrong? How do I achieve executing the last line of code only after all the activities before it have completed execution?

Old Geezer
  • 14,854
  • 31
  • 111
  • 198
  • Please check following link I hope it will helpful https://stackoverflow.com/questions/45876514/async-function-await-not-waiting-for- promise – Hardik Masalawala Jul 16 '19 at 11:39

3 Answers3

5

You're calling the (async () => {... function, which is, well, asynchronous, and then directly proceed to print the loading message.

You're also mixing and matching .then().catch() and async/await/catch error handling – I'd refactor things like this:

import sql = require("mssql");

const connect: (config: sql.config) => Promise<sql.ConnectionPool> = async config =>
  new Promise((resolve, reject) => {
    const pool = new sql.ConnectionPool(config);
    pool.connect(err => {
      if (err) return reject(err);
      console.log("CONNECTED");
      resolve(pool);
    });
  });

const config: sql.config = {
  /*...*/
};

(async () => {
  console.log("Now proceeding to load...");
  try {
    const pool = await connect(config);
    console.log("Connection pool created successfully.");
  } catch (e) {
    console.error(e);
    return;
  }
})();

AKX
  • 152,115
  • 15
  • 115
  • 172
  • I used that last `(async () => { ... })();` because I was struggling to execute an `await` invocation at the top level. Now I realize that won't work. I am aware your method works, but I was trying to avoid indent hell as the main code that follows in the module is huge and I was trying to achieve a "synchronous" syntax. – Old Geezer Jul 16 '19 at 13:16
2

Try this:

(async () => {
   await connect();
   console.log("Connection pool created successfully.");
})();
0

try something like below

import sql = require("mssql");
const config: sql.config = { /*....*/ };

const connect = () => {
    return new Promise((resolve, reject) => {
            try {
                let Connection = await sql.ConnectionPool(config).connect();
                console.log("Connected to mssql");
                resolve("Successfully Connected");
            } catch (error) {
                reject(error);
            }
    });
};

(async function () {
    try {
        let Connection = await connect();
        console.log("Connection pool created successfully.");
    } catch (error) {
        console.error(error);
    }
}());
Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54