external function call, promise, async and Mongo - confused












1















Beginning to feel really thick here. Read a lot and I believe I understand promises and async-await decently well. However, I seem to struggle to use the function elsewhere, such that I can obtain the result (e.g. i get pending in another js file with: let dbConnection = dbOperations.openDatabaseConnection();).



Could someone explain to me why do I keep getting pending from the below functions (same function written with promise and asyncawait)? I can console.log the dbConnection result as expected prior to my return within the function. Also, I am particularly keen to understand promises in this sense, as it seems that many npm packages seem to return promises (and with my experience at least the async-await does not sit well with that? -> using async does not wait for resolve in my experience).



// Establish database connection



function openDatabaseConnection() {

let dbConnection = {};

return mongodb.connect(dbUri).then(conn => {
dbConnection.connection = conn;
return dbConnection;
})
.then(() => {
dbConnection.session = dbConnection.connection.db(dbName);
//console.log(dbConnection);
return dbConnection;
})
.catch(err => {
throw err;
});
};


// Establish database connection



async function openDatabaseConnection() {

let dbConnection = {};

try {
dbConnection.connection = await mongodb.connect(dbUri);
dbConnection.session = await dbConnection.connection.db(dbName);
} finally {
//console.log(dbConnection);
return dbConnection;
};
};









share|improve this question

























  • What does "I keep getting pending" mean?

    – Liam
    Nov 22 '18 at 16:25











  • It'd make more sens if your then accepted the returned connection, then you don't need the let dbConnection, i.e. .then((dbConnection) => {dbConnection.session...

    – Liam
    Nov 22 '18 at 16:27











  • Thanks Liam, noticed you contributed to the answer below as well which explains this very well

    – Jontsu
    Nov 22 '18 at 18:45
















1















Beginning to feel really thick here. Read a lot and I believe I understand promises and async-await decently well. However, I seem to struggle to use the function elsewhere, such that I can obtain the result (e.g. i get pending in another js file with: let dbConnection = dbOperations.openDatabaseConnection();).



Could someone explain to me why do I keep getting pending from the below functions (same function written with promise and asyncawait)? I can console.log the dbConnection result as expected prior to my return within the function. Also, I am particularly keen to understand promises in this sense, as it seems that many npm packages seem to return promises (and with my experience at least the async-await does not sit well with that? -> using async does not wait for resolve in my experience).



// Establish database connection



function openDatabaseConnection() {

let dbConnection = {};

return mongodb.connect(dbUri).then(conn => {
dbConnection.connection = conn;
return dbConnection;
})
.then(() => {
dbConnection.session = dbConnection.connection.db(dbName);
//console.log(dbConnection);
return dbConnection;
})
.catch(err => {
throw err;
});
};


// Establish database connection



async function openDatabaseConnection() {

let dbConnection = {};

try {
dbConnection.connection = await mongodb.connect(dbUri);
dbConnection.session = await dbConnection.connection.db(dbName);
} finally {
//console.log(dbConnection);
return dbConnection;
};
};









share|improve this question

























  • What does "I keep getting pending" mean?

    – Liam
    Nov 22 '18 at 16:25











  • It'd make more sens if your then accepted the returned connection, then you don't need the let dbConnection, i.e. .then((dbConnection) => {dbConnection.session...

    – Liam
    Nov 22 '18 at 16:27











  • Thanks Liam, noticed you contributed to the answer below as well which explains this very well

    – Jontsu
    Nov 22 '18 at 18:45














1












1








1








Beginning to feel really thick here. Read a lot and I believe I understand promises and async-await decently well. However, I seem to struggle to use the function elsewhere, such that I can obtain the result (e.g. i get pending in another js file with: let dbConnection = dbOperations.openDatabaseConnection();).



Could someone explain to me why do I keep getting pending from the below functions (same function written with promise and asyncawait)? I can console.log the dbConnection result as expected prior to my return within the function. Also, I am particularly keen to understand promises in this sense, as it seems that many npm packages seem to return promises (and with my experience at least the async-await does not sit well with that? -> using async does not wait for resolve in my experience).



// Establish database connection



function openDatabaseConnection() {

let dbConnection = {};

return mongodb.connect(dbUri).then(conn => {
dbConnection.connection = conn;
return dbConnection;
})
.then(() => {
dbConnection.session = dbConnection.connection.db(dbName);
//console.log(dbConnection);
return dbConnection;
})
.catch(err => {
throw err;
});
};


// Establish database connection



async function openDatabaseConnection() {

let dbConnection = {};

try {
dbConnection.connection = await mongodb.connect(dbUri);
dbConnection.session = await dbConnection.connection.db(dbName);
} finally {
//console.log(dbConnection);
return dbConnection;
};
};









share|improve this question
















Beginning to feel really thick here. Read a lot and I believe I understand promises and async-await decently well. However, I seem to struggle to use the function elsewhere, such that I can obtain the result (e.g. i get pending in another js file with: let dbConnection = dbOperations.openDatabaseConnection();).



Could someone explain to me why do I keep getting pending from the below functions (same function written with promise and asyncawait)? I can console.log the dbConnection result as expected prior to my return within the function. Also, I am particularly keen to understand promises in this sense, as it seems that many npm packages seem to return promises (and with my experience at least the async-await does not sit well with that? -> using async does not wait for resolve in my experience).



// Establish database connection



function openDatabaseConnection() {

let dbConnection = {};

return mongodb.connect(dbUri).then(conn => {
dbConnection.connection = conn;
return dbConnection;
})
.then(() => {
dbConnection.session = dbConnection.connection.db(dbName);
//console.log(dbConnection);
return dbConnection;
})
.catch(err => {
throw err;
});
};


// Establish database connection



async function openDatabaseConnection() {

let dbConnection = {};

try {
dbConnection.connection = await mongodb.connect(dbUri);
dbConnection.session = await dbConnection.connection.db(dbName);
} finally {
//console.log(dbConnection);
return dbConnection;
};
};






javascript mongodb promise async-await






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 16:20









Anthony Winzlet

15.4k41339




15.4k41339










asked Nov 22 '18 at 16:09









JontsuJontsu

285




285













  • What does "I keep getting pending" mean?

    – Liam
    Nov 22 '18 at 16:25











  • It'd make more sens if your then accepted the returned connection, then you don't need the let dbConnection, i.e. .then((dbConnection) => {dbConnection.session...

    – Liam
    Nov 22 '18 at 16:27











  • Thanks Liam, noticed you contributed to the answer below as well which explains this very well

    – Jontsu
    Nov 22 '18 at 18:45



















  • What does "I keep getting pending" mean?

    – Liam
    Nov 22 '18 at 16:25











  • It'd make more sens if your then accepted the returned connection, then you don't need the let dbConnection, i.e. .then((dbConnection) => {dbConnection.session...

    – Liam
    Nov 22 '18 at 16:27











  • Thanks Liam, noticed you contributed to the answer below as well which explains this very well

    – Jontsu
    Nov 22 '18 at 18:45

















What does "I keep getting pending" mean?

– Liam
Nov 22 '18 at 16:25





What does "I keep getting pending" mean?

– Liam
Nov 22 '18 at 16:25













It'd make more sens if your then accepted the returned connection, then you don't need the let dbConnection, i.e. .then((dbConnection) => {dbConnection.session...

– Liam
Nov 22 '18 at 16:27





It'd make more sens if your then accepted the returned connection, then you don't need the let dbConnection, i.e. .then((dbConnection) => {dbConnection.session...

– Liam
Nov 22 '18 at 16:27













Thanks Liam, noticed you contributed to the answer below as well which explains this very well

– Jontsu
Nov 22 '18 at 18:45





Thanks Liam, noticed you contributed to the answer below as well which explains this very well

– Jontsu
Nov 22 '18 at 18:45












2 Answers
2






active

oldest

votes


















1














Both functions return again a promise.



So in your statement let dbConnection = dbOperations.openDatabaseConnection();
you assign a promise.



Thus you need to do something like:



dbOperations.openDatabaseConnection().then((dbConn) => ..)


or



let dbConnection = await dbOperations.openDatabaseConnection(); 


(note this requires to be wrapped in an async function)






share|improve this answer


























  • Thank you! Very clear, seems so obvious now. One additional question related to the difference between using promises and async-await. The await above seems so very neat, in the sense that I can declare a variable in a way that it is visible throughout my file. Is this possible with promises, or is it that e.g. above to use the dbConn I would have to operate within the arrow function? or is it possible to extract the variable outside of that function? Wanted to upvote, but not enough rep yet to do so

    – Jontsu
    Nov 22 '18 at 18:44











  • that's actually the usage of async/await - just syntax sugar that allows you to write asynchronous (promised) code in a synchronous fashion - does not offer any other advantage compared to native promises. With async await your variable is also just visible in the current async function scope, not that different as in .then((dbConn) => { .. }) right? :)

    – Kristianmitk
    Nov 22 '18 at 20:32













  • ah, did not realise :) still drilling on the promise point, so that it would be equivalently usable as the async where I can: let dbConnection = await..., would it simply be: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => ..)?

    – Jontsu
    Nov 22 '18 at 21:34











  • Trying it this way it did not seem to work at least (albeit did not try many variations, aside from return) -> leads to pending again when logging it outside of that function (whereas with async I can log it outside of the function e.g. below the let dbConnection = await...). With promise one of the tried ones: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => { dbConnection = dbConn; }); console.log(dbConnection);

    – Jontsu
    Nov 22 '18 at 21:44











  • With promises you have the fulfilled data only available in the function - therefore you pass it, so upon settlement the function may be called with the data. With the support of Iterators/Generators in JS (ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…) async/await was added simply as syntax sugar. Did you already check promisejs.org/generators ? Could help you to better understand promises and with generators/iterators how async/await works

    – Kristianmitk
    Nov 22 '18 at 22:11



















0














Async/await is just another way to work with Promises, just don't wait for something that isn't a Promise.



async function openDatabaseConnection() {

let dbConnection = {};

try {
dbConnection.connection = await mongodb.connect(dbUri);
// await here does not make sense, this function does not return a Promise
// dbConnection.session = await dbConnection.connection.db(dbName);
dbConnection.session = dbConnection.connection.db(dbName);
} finally {
//console.log(dbConnection);
// return will always execute, keep here only when it should
// return an empty object if the connection fails
return dbConnection;
};
};


More info on async/await






share|improve this answer
























  • Thank you! Good comments to help me understand parts that seem to be shady still on these.

    – Jontsu
    Nov 22 '18 at 18:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434756%2fexternal-function-call-promise-async-and-mongo-confused%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Both functions return again a promise.



So in your statement let dbConnection = dbOperations.openDatabaseConnection();
you assign a promise.



Thus you need to do something like:



dbOperations.openDatabaseConnection().then((dbConn) => ..)


or



let dbConnection = await dbOperations.openDatabaseConnection(); 


(note this requires to be wrapped in an async function)






share|improve this answer


























  • Thank you! Very clear, seems so obvious now. One additional question related to the difference between using promises and async-await. The await above seems so very neat, in the sense that I can declare a variable in a way that it is visible throughout my file. Is this possible with promises, or is it that e.g. above to use the dbConn I would have to operate within the arrow function? or is it possible to extract the variable outside of that function? Wanted to upvote, but not enough rep yet to do so

    – Jontsu
    Nov 22 '18 at 18:44











  • that's actually the usage of async/await - just syntax sugar that allows you to write asynchronous (promised) code in a synchronous fashion - does not offer any other advantage compared to native promises. With async await your variable is also just visible in the current async function scope, not that different as in .then((dbConn) => { .. }) right? :)

    – Kristianmitk
    Nov 22 '18 at 20:32













  • ah, did not realise :) still drilling on the promise point, so that it would be equivalently usable as the async where I can: let dbConnection = await..., would it simply be: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => ..)?

    – Jontsu
    Nov 22 '18 at 21:34











  • Trying it this way it did not seem to work at least (albeit did not try many variations, aside from return) -> leads to pending again when logging it outside of that function (whereas with async I can log it outside of the function e.g. below the let dbConnection = await...). With promise one of the tried ones: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => { dbConnection = dbConn; }); console.log(dbConnection);

    – Jontsu
    Nov 22 '18 at 21:44











  • With promises you have the fulfilled data only available in the function - therefore you pass it, so upon settlement the function may be called with the data. With the support of Iterators/Generators in JS (ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…) async/await was added simply as syntax sugar. Did you already check promisejs.org/generators ? Could help you to better understand promises and with generators/iterators how async/await works

    – Kristianmitk
    Nov 22 '18 at 22:11
















1














Both functions return again a promise.



So in your statement let dbConnection = dbOperations.openDatabaseConnection();
you assign a promise.



Thus you need to do something like:



dbOperations.openDatabaseConnection().then((dbConn) => ..)


or



let dbConnection = await dbOperations.openDatabaseConnection(); 


(note this requires to be wrapped in an async function)






share|improve this answer


























  • Thank you! Very clear, seems so obvious now. One additional question related to the difference between using promises and async-await. The await above seems so very neat, in the sense that I can declare a variable in a way that it is visible throughout my file. Is this possible with promises, or is it that e.g. above to use the dbConn I would have to operate within the arrow function? or is it possible to extract the variable outside of that function? Wanted to upvote, but not enough rep yet to do so

    – Jontsu
    Nov 22 '18 at 18:44











  • that's actually the usage of async/await - just syntax sugar that allows you to write asynchronous (promised) code in a synchronous fashion - does not offer any other advantage compared to native promises. With async await your variable is also just visible in the current async function scope, not that different as in .then((dbConn) => { .. }) right? :)

    – Kristianmitk
    Nov 22 '18 at 20:32













  • ah, did not realise :) still drilling on the promise point, so that it would be equivalently usable as the async where I can: let dbConnection = await..., would it simply be: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => ..)?

    – Jontsu
    Nov 22 '18 at 21:34











  • Trying it this way it did not seem to work at least (albeit did not try many variations, aside from return) -> leads to pending again when logging it outside of that function (whereas with async I can log it outside of the function e.g. below the let dbConnection = await...). With promise one of the tried ones: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => { dbConnection = dbConn; }); console.log(dbConnection);

    – Jontsu
    Nov 22 '18 at 21:44











  • With promises you have the fulfilled data only available in the function - therefore you pass it, so upon settlement the function may be called with the data. With the support of Iterators/Generators in JS (ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…) async/await was added simply as syntax sugar. Did you already check promisejs.org/generators ? Could help you to better understand promises and with generators/iterators how async/await works

    – Kristianmitk
    Nov 22 '18 at 22:11














1












1








1







Both functions return again a promise.



So in your statement let dbConnection = dbOperations.openDatabaseConnection();
you assign a promise.



Thus you need to do something like:



dbOperations.openDatabaseConnection().then((dbConn) => ..)


or



let dbConnection = await dbOperations.openDatabaseConnection(); 


(note this requires to be wrapped in an async function)






share|improve this answer















Both functions return again a promise.



So in your statement let dbConnection = dbOperations.openDatabaseConnection();
you assign a promise.



Thus you need to do something like:



dbOperations.openDatabaseConnection().then((dbConn) => ..)


or



let dbConnection = await dbOperations.openDatabaseConnection(); 


(note this requires to be wrapped in an async function)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 16:28









Liam

16.2k1676129




16.2k1676129










answered Nov 22 '18 at 16:27









KristianmitkKristianmitk

2,51731233




2,51731233













  • Thank you! Very clear, seems so obvious now. One additional question related to the difference between using promises and async-await. The await above seems so very neat, in the sense that I can declare a variable in a way that it is visible throughout my file. Is this possible with promises, or is it that e.g. above to use the dbConn I would have to operate within the arrow function? or is it possible to extract the variable outside of that function? Wanted to upvote, but not enough rep yet to do so

    – Jontsu
    Nov 22 '18 at 18:44











  • that's actually the usage of async/await - just syntax sugar that allows you to write asynchronous (promised) code in a synchronous fashion - does not offer any other advantage compared to native promises. With async await your variable is also just visible in the current async function scope, not that different as in .then((dbConn) => { .. }) right? :)

    – Kristianmitk
    Nov 22 '18 at 20:32













  • ah, did not realise :) still drilling on the promise point, so that it would be equivalently usable as the async where I can: let dbConnection = await..., would it simply be: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => ..)?

    – Jontsu
    Nov 22 '18 at 21:34











  • Trying it this way it did not seem to work at least (albeit did not try many variations, aside from return) -> leads to pending again when logging it outside of that function (whereas with async I can log it outside of the function e.g. below the let dbConnection = await...). With promise one of the tried ones: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => { dbConnection = dbConn; }); console.log(dbConnection);

    – Jontsu
    Nov 22 '18 at 21:44











  • With promises you have the fulfilled data only available in the function - therefore you pass it, so upon settlement the function may be called with the data. With the support of Iterators/Generators in JS (ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…) async/await was added simply as syntax sugar. Did you already check promisejs.org/generators ? Could help you to better understand promises and with generators/iterators how async/await works

    – Kristianmitk
    Nov 22 '18 at 22:11



















  • Thank you! Very clear, seems so obvious now. One additional question related to the difference between using promises and async-await. The await above seems so very neat, in the sense that I can declare a variable in a way that it is visible throughout my file. Is this possible with promises, or is it that e.g. above to use the dbConn I would have to operate within the arrow function? or is it possible to extract the variable outside of that function? Wanted to upvote, but not enough rep yet to do so

    – Jontsu
    Nov 22 '18 at 18:44











  • that's actually the usage of async/await - just syntax sugar that allows you to write asynchronous (promised) code in a synchronous fashion - does not offer any other advantage compared to native promises. With async await your variable is also just visible in the current async function scope, not that different as in .then((dbConn) => { .. }) right? :)

    – Kristianmitk
    Nov 22 '18 at 20:32













  • ah, did not realise :) still drilling on the promise point, so that it would be equivalently usable as the async where I can: let dbConnection = await..., would it simply be: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => ..)?

    – Jontsu
    Nov 22 '18 at 21:34











  • Trying it this way it did not seem to work at least (albeit did not try many variations, aside from return) -> leads to pending again when logging it outside of that function (whereas with async I can log it outside of the function e.g. below the let dbConnection = await...). With promise one of the tried ones: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => { dbConnection = dbConn; }); console.log(dbConnection);

    – Jontsu
    Nov 22 '18 at 21:44











  • With promises you have the fulfilled data only available in the function - therefore you pass it, so upon settlement the function may be called with the data. With the support of Iterators/Generators in JS (ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…) async/await was added simply as syntax sugar. Did you already check promisejs.org/generators ? Could help you to better understand promises and with generators/iterators how async/await works

    – Kristianmitk
    Nov 22 '18 at 22:11

















Thank you! Very clear, seems so obvious now. One additional question related to the difference between using promises and async-await. The await above seems so very neat, in the sense that I can declare a variable in a way that it is visible throughout my file. Is this possible with promises, or is it that e.g. above to use the dbConn I would have to operate within the arrow function? or is it possible to extract the variable outside of that function? Wanted to upvote, but not enough rep yet to do so

– Jontsu
Nov 22 '18 at 18:44





Thank you! Very clear, seems so obvious now. One additional question related to the difference between using promises and async-await. The await above seems so very neat, in the sense that I can declare a variable in a way that it is visible throughout my file. Is this possible with promises, or is it that e.g. above to use the dbConn I would have to operate within the arrow function? or is it possible to extract the variable outside of that function? Wanted to upvote, but not enough rep yet to do so

– Jontsu
Nov 22 '18 at 18:44













that's actually the usage of async/await - just syntax sugar that allows you to write asynchronous (promised) code in a synchronous fashion - does not offer any other advantage compared to native promises. With async await your variable is also just visible in the current async function scope, not that different as in .then((dbConn) => { .. }) right? :)

– Kristianmitk
Nov 22 '18 at 20:32







that's actually the usage of async/await - just syntax sugar that allows you to write asynchronous (promised) code in a synchronous fashion - does not offer any other advantage compared to native promises. With async await your variable is also just visible in the current async function scope, not that different as in .then((dbConn) => { .. }) right? :)

– Kristianmitk
Nov 22 '18 at 20:32















ah, did not realise :) still drilling on the promise point, so that it would be equivalently usable as the async where I can: let dbConnection = await..., would it simply be: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => ..)?

– Jontsu
Nov 22 '18 at 21:34





ah, did not realise :) still drilling on the promise point, so that it would be equivalently usable as the async where I can: let dbConnection = await..., would it simply be: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => ..)?

– Jontsu
Nov 22 '18 at 21:34













Trying it this way it did not seem to work at least (albeit did not try many variations, aside from return) -> leads to pending again when logging it outside of that function (whereas with async I can log it outside of the function e.g. below the let dbConnection = await...). With promise one of the tried ones: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => { dbConnection = dbConn; }); console.log(dbConnection);

– Jontsu
Nov 22 '18 at 21:44





Trying it this way it did not seem to work at least (albeit did not try many variations, aside from return) -> leads to pending again when logging it outside of that function (whereas with async I can log it outside of the function e.g. below the let dbConnection = await...). With promise one of the tried ones: let dbConnection = dbOperations.openDatabaseConnection().then((dbConn) => { dbConnection = dbConn; }); console.log(dbConnection);

– Jontsu
Nov 22 '18 at 21:44













With promises you have the fulfilled data only available in the function - therefore you pass it, so upon settlement the function may be called with the data. With the support of Iterators/Generators in JS (ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…) async/await was added simply as syntax sugar. Did you already check promisejs.org/generators ? Could help you to better understand promises and with generators/iterators how async/await works

– Kristianmitk
Nov 22 '18 at 22:11





With promises you have the fulfilled data only available in the function - therefore you pass it, so upon settlement the function may be called with the data. With the support of Iterators/Generators in JS (ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…) async/await was added simply as syntax sugar. Did you already check promisejs.org/generators ? Could help you to better understand promises and with generators/iterators how async/await works

– Kristianmitk
Nov 22 '18 at 22:11













0














Async/await is just another way to work with Promises, just don't wait for something that isn't a Promise.



async function openDatabaseConnection() {

let dbConnection = {};

try {
dbConnection.connection = await mongodb.connect(dbUri);
// await here does not make sense, this function does not return a Promise
// dbConnection.session = await dbConnection.connection.db(dbName);
dbConnection.session = dbConnection.connection.db(dbName);
} finally {
//console.log(dbConnection);
// return will always execute, keep here only when it should
// return an empty object if the connection fails
return dbConnection;
};
};


More info on async/await






share|improve this answer
























  • Thank you! Good comments to help me understand parts that seem to be shady still on these.

    – Jontsu
    Nov 22 '18 at 18:46
















0














Async/await is just another way to work with Promises, just don't wait for something that isn't a Promise.



async function openDatabaseConnection() {

let dbConnection = {};

try {
dbConnection.connection = await mongodb.connect(dbUri);
// await here does not make sense, this function does not return a Promise
// dbConnection.session = await dbConnection.connection.db(dbName);
dbConnection.session = dbConnection.connection.db(dbName);
} finally {
//console.log(dbConnection);
// return will always execute, keep here only when it should
// return an empty object if the connection fails
return dbConnection;
};
};


More info on async/await






share|improve this answer
























  • Thank you! Good comments to help me understand parts that seem to be shady still on these.

    – Jontsu
    Nov 22 '18 at 18:46














0












0








0







Async/await is just another way to work with Promises, just don't wait for something that isn't a Promise.



async function openDatabaseConnection() {

let dbConnection = {};

try {
dbConnection.connection = await mongodb.connect(dbUri);
// await here does not make sense, this function does not return a Promise
// dbConnection.session = await dbConnection.connection.db(dbName);
dbConnection.session = dbConnection.connection.db(dbName);
} finally {
//console.log(dbConnection);
// return will always execute, keep here only when it should
// return an empty object if the connection fails
return dbConnection;
};
};


More info on async/await






share|improve this answer













Async/await is just another way to work with Promises, just don't wait for something that isn't a Promise.



async function openDatabaseConnection() {

let dbConnection = {};

try {
dbConnection.connection = await mongodb.connect(dbUri);
// await here does not make sense, this function does not return a Promise
// dbConnection.session = await dbConnection.connection.db(dbName);
dbConnection.session = dbConnection.connection.db(dbName);
} finally {
//console.log(dbConnection);
// return will always execute, keep here only when it should
// return an empty object if the connection fails
return dbConnection;
};
};


More info on async/await







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 16:33









David LemonDavid Lemon

1,023718




1,023718













  • Thank you! Good comments to help me understand parts that seem to be shady still on these.

    – Jontsu
    Nov 22 '18 at 18:46



















  • Thank you! Good comments to help me understand parts that seem to be shady still on these.

    – Jontsu
    Nov 22 '18 at 18:46

















Thank you! Good comments to help me understand parts that seem to be shady still on these.

– Jontsu
Nov 22 '18 at 18:46





Thank you! Good comments to help me understand parts that seem to be shady still on these.

– Jontsu
Nov 22 '18 at 18:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434756%2fexternal-function-call-promise-async-and-mongo-confused%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen