Promise.all is not working/waiting JavaScript in combination with mongoose
I got a problem that the await Promise.all
is not working in my case. I attached the code and the output I got:
await Promise.all(allIndizes.map(async (index) => {
await axios.get(uri)
.then(async function (response) {
let searchResult = response.data.hits.hits;
console.log('Search Result: ' + searchResult);
await Promise.all(searchResult.map(async (element) => {
await primaryKeyModel.findById(element._id).exec((err, pk) => {
console.log('PK, direct after search: ' + pk);
//DO SOME STUFF HERE BUT DELETED IT TO SHORTEN THE CODE
}
})
console.log('test1');
}));
})
console.log('test2');
}));
The output is the following:
test1
test2
PK, direct after search: { _id: 5bf1c0619674e2052a4f6a64 ... }
I actually would actually expect that the first output is the 'PK, direct after search'. I don't understand why the function is not waiting? Do someone has a hint, whats wrong here? I found a similar issue here and I adopted the logic but its still not working. Thanks for the help.
I tried to shorten the code as much as possible. I only deleted statements which are not affecting the async execution.
javascript node.js mongoose
add a comment |
I got a problem that the await Promise.all
is not working in my case. I attached the code and the output I got:
await Promise.all(allIndizes.map(async (index) => {
await axios.get(uri)
.then(async function (response) {
let searchResult = response.data.hits.hits;
console.log('Search Result: ' + searchResult);
await Promise.all(searchResult.map(async (element) => {
await primaryKeyModel.findById(element._id).exec((err, pk) => {
console.log('PK, direct after search: ' + pk);
//DO SOME STUFF HERE BUT DELETED IT TO SHORTEN THE CODE
}
})
console.log('test1');
}));
})
console.log('test2');
}));
The output is the following:
test1
test2
PK, direct after search: { _id: 5bf1c0619674e2052a4f6a64 ... }
I actually would actually expect that the first output is the 'PK, direct after search'. I don't understand why the function is not waiting? Do someone has a hint, whats wrong here? I found a similar issue here and I adopted the logic but its still not working. Thanks for the help.
I tried to shorten the code as much as possible. I only deleted statements which are not affecting the async execution.
javascript node.js mongoose
When you pass a callback to a mongoose function, it doesn't return a promise.
– Bergi
Nov 18 '18 at 20:48
Don't usethen
when you could useawait
instead!
– Bergi
Nov 18 '18 at 20:49
add a comment |
I got a problem that the await Promise.all
is not working in my case. I attached the code and the output I got:
await Promise.all(allIndizes.map(async (index) => {
await axios.get(uri)
.then(async function (response) {
let searchResult = response.data.hits.hits;
console.log('Search Result: ' + searchResult);
await Promise.all(searchResult.map(async (element) => {
await primaryKeyModel.findById(element._id).exec((err, pk) => {
console.log('PK, direct after search: ' + pk);
//DO SOME STUFF HERE BUT DELETED IT TO SHORTEN THE CODE
}
})
console.log('test1');
}));
})
console.log('test2');
}));
The output is the following:
test1
test2
PK, direct after search: { _id: 5bf1c0619674e2052a4f6a64 ... }
I actually would actually expect that the first output is the 'PK, direct after search'. I don't understand why the function is not waiting? Do someone has a hint, whats wrong here? I found a similar issue here and I adopted the logic but its still not working. Thanks for the help.
I tried to shorten the code as much as possible. I only deleted statements which are not affecting the async execution.
javascript node.js mongoose
I got a problem that the await Promise.all
is not working in my case. I attached the code and the output I got:
await Promise.all(allIndizes.map(async (index) => {
await axios.get(uri)
.then(async function (response) {
let searchResult = response.data.hits.hits;
console.log('Search Result: ' + searchResult);
await Promise.all(searchResult.map(async (element) => {
await primaryKeyModel.findById(element._id).exec((err, pk) => {
console.log('PK, direct after search: ' + pk);
//DO SOME STUFF HERE BUT DELETED IT TO SHORTEN THE CODE
}
})
console.log('test1');
}));
})
console.log('test2');
}));
The output is the following:
test1
test2
PK, direct after search: { _id: 5bf1c0619674e2052a4f6a64 ... }
I actually would actually expect that the first output is the 'PK, direct after search'. I don't understand why the function is not waiting? Do someone has a hint, whats wrong here? I found a similar issue here and I adopted the logic but its still not working. Thanks for the help.
I tried to shorten the code as much as possible. I only deleted statements which are not affecting the async execution.
javascript node.js mongoose
javascript node.js mongoose
edited Nov 18 '18 at 20:10
Dacre Denny
12k41031
12k41031
asked Nov 18 '18 at 20:04
MaxMax
31
31
When you pass a callback to a mongoose function, it doesn't return a promise.
– Bergi
Nov 18 '18 at 20:48
Don't usethen
when you could useawait
instead!
– Bergi
Nov 18 '18 at 20:49
add a comment |
When you pass a callback to a mongoose function, it doesn't return a promise.
– Bergi
Nov 18 '18 at 20:48
Don't usethen
when you could useawait
instead!
– Bergi
Nov 18 '18 at 20:49
When you pass a callback to a mongoose function, it doesn't return a promise.
– Bergi
Nov 18 '18 at 20:48
When you pass a callback to a mongoose function, it doesn't return a promise.
– Bergi
Nov 18 '18 at 20:48
Don't use
then
when you could use await
instead!– Bergi
Nov 18 '18 at 20:49
Don't use
then
when you could use await
instead!– Bergi
Nov 18 '18 at 20:49
add a comment |
1 Answer
1
active
oldest
votes
Mongoose supports promises for a long time, callback-based API is obsolete, it's a mistake to use it where a promise is expected (await
).
then
is unwanted inside of async
functions, this defies the purpose of using async..await
.
It should be:
await Promise.all(allIndizes.map(async (index) => {
const response = await axios.get(uri);
let searchResult = response.data.hits.hits;
await Promise.all(searchResult.map(async (element) => {
const pk = await primaryKeyModel.findById(element._id);
//DO SOME STUFF HERE BUT DELETED IT TO SHORTEN THE CODE
}));
}));
Thanks a lot, its working now
– Max
Nov 18 '18 at 20:34
Glad it helped.
– estus
Nov 18 '18 at 20:35
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364928%2fpromise-all-is-not-working-waiting-javascript-in-combination-with-mongoose%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
Mongoose supports promises for a long time, callback-based API is obsolete, it's a mistake to use it where a promise is expected (await
).
then
is unwanted inside of async
functions, this defies the purpose of using async..await
.
It should be:
await Promise.all(allIndizes.map(async (index) => {
const response = await axios.get(uri);
let searchResult = response.data.hits.hits;
await Promise.all(searchResult.map(async (element) => {
const pk = await primaryKeyModel.findById(element._id);
//DO SOME STUFF HERE BUT DELETED IT TO SHORTEN THE CODE
}));
}));
Thanks a lot, its working now
– Max
Nov 18 '18 at 20:34
Glad it helped.
– estus
Nov 18 '18 at 20:35
add a comment |
Mongoose supports promises for a long time, callback-based API is obsolete, it's a mistake to use it where a promise is expected (await
).
then
is unwanted inside of async
functions, this defies the purpose of using async..await
.
It should be:
await Promise.all(allIndizes.map(async (index) => {
const response = await axios.get(uri);
let searchResult = response.data.hits.hits;
await Promise.all(searchResult.map(async (element) => {
const pk = await primaryKeyModel.findById(element._id);
//DO SOME STUFF HERE BUT DELETED IT TO SHORTEN THE CODE
}));
}));
Thanks a lot, its working now
– Max
Nov 18 '18 at 20:34
Glad it helped.
– estus
Nov 18 '18 at 20:35
add a comment |
Mongoose supports promises for a long time, callback-based API is obsolete, it's a mistake to use it where a promise is expected (await
).
then
is unwanted inside of async
functions, this defies the purpose of using async..await
.
It should be:
await Promise.all(allIndizes.map(async (index) => {
const response = await axios.get(uri);
let searchResult = response.data.hits.hits;
await Promise.all(searchResult.map(async (element) => {
const pk = await primaryKeyModel.findById(element._id);
//DO SOME STUFF HERE BUT DELETED IT TO SHORTEN THE CODE
}));
}));
Mongoose supports promises for a long time, callback-based API is obsolete, it's a mistake to use it where a promise is expected (await
).
then
is unwanted inside of async
functions, this defies the purpose of using async..await
.
It should be:
await Promise.all(allIndizes.map(async (index) => {
const response = await axios.get(uri);
let searchResult = response.data.hits.hits;
await Promise.all(searchResult.map(async (element) => {
const pk = await primaryKeyModel.findById(element._id);
//DO SOME STUFF HERE BUT DELETED IT TO SHORTEN THE CODE
}));
}));
answered Nov 18 '18 at 20:17
estusestus
71.7k22106222
71.7k22106222
Thanks a lot, its working now
– Max
Nov 18 '18 at 20:34
Glad it helped.
– estus
Nov 18 '18 at 20:35
add a comment |
Thanks a lot, its working now
– Max
Nov 18 '18 at 20:34
Glad it helped.
– estus
Nov 18 '18 at 20:35
Thanks a lot, its working now
– Max
Nov 18 '18 at 20:34
Thanks a lot, its working now
– Max
Nov 18 '18 at 20:34
Glad it helped.
– estus
Nov 18 '18 at 20:35
Glad it helped.
– estus
Nov 18 '18 at 20:35
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364928%2fpromise-all-is-not-working-waiting-javascript-in-combination-with-mongoose%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
When you pass a callback to a mongoose function, it doesn't return a promise.
– Bergi
Nov 18 '18 at 20:48
Don't use
then
when you could useawait
instead!– Bergi
Nov 18 '18 at 20:49