foo();
function foo(){
let fooPromise = new Promise(async function(resolve, reject) {
var dbConn = new sql.ConnectionPool(config);
dbConn.connect().then(function () {
var request = new sql.Request(dbConn);
request.query(`SELECT TOP 1 someDate FROM Stable `).then(function (recordSet) {
var lastSaledate = recordSet.recordset[0].someDate;
return resolve(lastSaledate);
}).catch(function (err) {
return reject(err);
});
});
});
let fooPromisereturn = await fooPromise;
console.log(fooPromisereturn);
return fooPromisereturn // I want this value to be return whlile calling foo()
}
Asked
Active
Viewed 52 times
0

node_modules
- 4,790
- 6
- 21
- 37
-
First of all. When you assign async over a function its implied that it will return a promise so using new Promise over it doesn't make any sense. also you need to await for the dbconnection – innocent Oct 05 '22 at 06:18
-
You can only `await fooPromise` inside `foo` if you make `foo` `async`. When `foo` is `async` it'll need to be `await`ed as well. This is because you cannot return a value which will be available *sometime later* **right now**. You *have* to `await` values which will only be available *sometime later*. – deceze Oct 05 '22 at 06:27
-
[Never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572)! – Bergi Oct 05 '22 at 09:45