Promise.all is not working/waiting JavaScript in combination with mongoose












0















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.










share|improve this question

























  • 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
















0















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.










share|improve this question

























  • 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














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 use then when you could use await 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











  • Don't use then when you could use await 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












1 Answer
1






active

oldest

votes


















0














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
}));
}));





share|improve this answer
























  • Thanks a lot, its working now

    – Max
    Nov 18 '18 at 20:34











  • Glad it helped.

    – estus
    Nov 18 '18 at 20:35











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%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









0














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
}));
}));





share|improve this answer
























  • Thanks a lot, its working now

    – Max
    Nov 18 '18 at 20:34











  • Glad it helped.

    – estus
    Nov 18 '18 at 20:35
















0














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
}));
}));





share|improve this answer
























  • Thanks a lot, its working now

    – Max
    Nov 18 '18 at 20:34











  • Glad it helped.

    – estus
    Nov 18 '18 at 20:35














0












0








0







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
}));
}));





share|improve this answer













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
}));
}));






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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




















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%2f53364928%2fpromise-all-is-not-working-waiting-javascript-in-combination-with-mongoose%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()