Why my Async and Await still not do the job of sequence execution











up vote
0
down vote

favorite












I'm a newbie to Node.js and not sure if I understood Async/Await correctly. Here's what I'm trying to do



I'm passing values to a function and I'm expecting it to work like



Step 1. add both values
Step 2. Fetch a document from db based on value from step 1
Step 3. return the document fetched from step 2.



But the result I see is



Step 1. add both values
Step 2. return undefined while db query is running
step 3. finish running query



How do I achieve desired output from this.



var someFunction = async (a,b)=>{
var k;
try{
k = a+b;
}catch(err){
return(err);
}

var document;
try{
document = await db.collection(somecollection).findOne({_id:k})
}catch(err){
return(err);
}
return(document);
}

someFunction(4,5).then((result)=>{
console.log(result);
});









share|improve this question






















  • Which database are you using? Are you sure that db.collection(somecollection).findOne({_id:k}) returns a promise?
    – jfriend00
    Nov 15 at 3:01

















up vote
0
down vote

favorite












I'm a newbie to Node.js and not sure if I understood Async/Await correctly. Here's what I'm trying to do



I'm passing values to a function and I'm expecting it to work like



Step 1. add both values
Step 2. Fetch a document from db based on value from step 1
Step 3. return the document fetched from step 2.



But the result I see is



Step 1. add both values
Step 2. return undefined while db query is running
step 3. finish running query



How do I achieve desired output from this.



var someFunction = async (a,b)=>{
var k;
try{
k = a+b;
}catch(err){
return(err);
}

var document;
try{
document = await db.collection(somecollection).findOne({_id:k})
}catch(err){
return(err);
}
return(document);
}

someFunction(4,5).then((result)=>{
console.log(result);
});









share|improve this question






















  • Which database are you using? Are you sure that db.collection(somecollection).findOne({_id:k}) returns a promise?
    – jfriend00
    Nov 15 at 3:01















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm a newbie to Node.js and not sure if I understood Async/Await correctly. Here's what I'm trying to do



I'm passing values to a function and I'm expecting it to work like



Step 1. add both values
Step 2. Fetch a document from db based on value from step 1
Step 3. return the document fetched from step 2.



But the result I see is



Step 1. add both values
Step 2. return undefined while db query is running
step 3. finish running query



How do I achieve desired output from this.



var someFunction = async (a,b)=>{
var k;
try{
k = a+b;
}catch(err){
return(err);
}

var document;
try{
document = await db.collection(somecollection).findOne({_id:k})
}catch(err){
return(err);
}
return(document);
}

someFunction(4,5).then((result)=>{
console.log(result);
});









share|improve this question













I'm a newbie to Node.js and not sure if I understood Async/Await correctly. Here's what I'm trying to do



I'm passing values to a function and I'm expecting it to work like



Step 1. add both values
Step 2. Fetch a document from db based on value from step 1
Step 3. return the document fetched from step 2.



But the result I see is



Step 1. add both values
Step 2. return undefined while db query is running
step 3. finish running query



How do I achieve desired output from this.



var someFunction = async (a,b)=>{
var k;
try{
k = a+b;
}catch(err){
return(err);
}

var document;
try{
document = await db.collection(somecollection).findOne({_id:k})
}catch(err){
return(err);
}
return(document);
}

someFunction(4,5).then((result)=>{
console.log(result);
});






javascript angularjs node.js async-await






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 at 1:59









David

106




106












  • Which database are you using? Are you sure that db.collection(somecollection).findOne({_id:k}) returns a promise?
    – jfriend00
    Nov 15 at 3:01




















  • Which database are you using? Are you sure that db.collection(somecollection).findOne({_id:k}) returns a promise?
    – jfriend00
    Nov 15 at 3:01


















Which database are you using? Are you sure that db.collection(somecollection).findOne({_id:k}) returns a promise?
– jfriend00
Nov 15 at 3:01






Which database are you using? Are you sure that db.collection(somecollection).findOne({_id:k}) returns a promise?
– jfriend00
Nov 15 at 3:01














1 Answer
1






active

oldest

votes

















up vote
1
down vote













findone may return undefined if no match found
findOne returns undefined on the server



In the first try...catch block, why do you worry about an exception of adding two variables? that never result on error in javascript.



Also make sure you're passing the _id correctly to findone, Mongo uses UUID to calculate _id and I don't think that can be returned from adding a + b






share|improve this answer























  • This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
    – David
    Nov 15 at 2:28












  • try to cast k to ObjectId, add var ObjectId = require('mongodb').ObjectId in top and then await db.collection(somecollection).findOne({_id: new ObjectId(k) })
    – doc_id
    Nov 15 at 4:47











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',
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%2f53311362%2fwhy-my-async-and-await-still-not-do-the-job-of-sequence-execution%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













findone may return undefined if no match found
findOne returns undefined on the server



In the first try...catch block, why do you worry about an exception of adding two variables? that never result on error in javascript.



Also make sure you're passing the _id correctly to findone, Mongo uses UUID to calculate _id and I don't think that can be returned from adding a + b






share|improve this answer























  • This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
    – David
    Nov 15 at 2:28












  • try to cast k to ObjectId, add var ObjectId = require('mongodb').ObjectId in top and then await db.collection(somecollection).findOne({_id: new ObjectId(k) })
    – doc_id
    Nov 15 at 4:47















up vote
1
down vote













findone may return undefined if no match found
findOne returns undefined on the server



In the first try...catch block, why do you worry about an exception of adding two variables? that never result on error in javascript.



Also make sure you're passing the _id correctly to findone, Mongo uses UUID to calculate _id and I don't think that can be returned from adding a + b






share|improve this answer























  • This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
    – David
    Nov 15 at 2:28












  • try to cast k to ObjectId, add var ObjectId = require('mongodb').ObjectId in top and then await db.collection(somecollection).findOne({_id: new ObjectId(k) })
    – doc_id
    Nov 15 at 4:47













up vote
1
down vote










up vote
1
down vote









findone may return undefined if no match found
findOne returns undefined on the server



In the first try...catch block, why do you worry about an exception of adding two variables? that never result on error in javascript.



Also make sure you're passing the _id correctly to findone, Mongo uses UUID to calculate _id and I don't think that can be returned from adding a + b






share|improve this answer














findone may return undefined if no match found
findOne returns undefined on the server



In the first try...catch block, why do you worry about an exception of adding two variables? that never result on error in javascript.



Also make sure you're passing the _id correctly to findone, Mongo uses UUID to calculate _id and I don't think that can be returned from adding a + b







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 at 2:18

























answered Nov 15 at 2:12









doc_id

874930




874930












  • This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
    – David
    Nov 15 at 2:28












  • try to cast k to ObjectId, add var ObjectId = require('mongodb').ObjectId in top and then await db.collection(somecollection).findOne({_id: new ObjectId(k) })
    – doc_id
    Nov 15 at 4:47


















  • This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
    – David
    Nov 15 at 2:28












  • try to cast k to ObjectId, add var ObjectId = require('mongodb').ObjectId in top and then await db.collection(somecollection).findOne({_id: new ObjectId(k) })
    – doc_id
    Nov 15 at 4:47
















This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
– David
Nov 15 at 2:28






This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
– David
Nov 15 at 2:28














try to cast k to ObjectId, add var ObjectId = require('mongodb').ObjectId in top and then await db.collection(somecollection).findOne({_id: new ObjectId(k) })
– doc_id
Nov 15 at 4:47




try to cast k to ObjectId, add var ObjectId = require('mongodb').ObjectId in top and then await db.collection(somecollection).findOne({_id: new ObjectId(k) })
– doc_id
Nov 15 at 4:47


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53311362%2fwhy-my-async-and-await-still-not-do-the-job-of-sequence-execution%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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents