Promise.all is not working/waiting JavaScript in combination with mongoose
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 at 20:48
Don't usethen
when you could useawait
instead!
– Bergi
Nov 18 at 20:49
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 20:10
Dacre Denny
8,3314729
8,3314729
asked Nov 18 at 20:04
Max
31
31
When you pass a callback to a mongoose function, it doesn't return a promise.
– Bergi
Nov 18 at 20:48
Don't usethen
when you could useawait
instead!
– Bergi
Nov 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 at 20:48
Don't usethen
when you could useawait
instead!
– Bergi
Nov 18 at 20:49
When you pass a callback to a mongoose function, it doesn't return a promise.
– Bergi
Nov 18 at 20:48
When you pass a callback to a mongoose function, it doesn't return a promise.
– Bergi
Nov 18 at 20:48
Don't use
then
when you could use await
instead!– Bergi
Nov 18 at 20:49
Don't use
then
when you could use await
instead!– Bergi
Nov 18 at 20:49
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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 at 20:34
Glad it helped.
– estus
Nov 18 at 20:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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 at 20:34
Glad it helped.
– estus
Nov 18 at 20:35
add a comment |
up vote
0
down vote
accepted
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 at 20:34
Glad it helped.
– estus
Nov 18 at 20:35
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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 at 20:17
estus
63.6k2193201
63.6k2193201
Thanks a lot, its working now
– Max
Nov 18 at 20:34
Glad it helped.
– estus
Nov 18 at 20:35
add a comment |
Thanks a lot, its working now
– Max
Nov 18 at 20:34
Glad it helped.
– estus
Nov 18 at 20:35
Thanks a lot, its working now
– Max
Nov 18 at 20:34
Thanks a lot, its working now
– Max
Nov 18 at 20:34
Glad it helped.
– estus
Nov 18 at 20:35
Glad it helped.
– estus
Nov 18 at 20:35
add a comment |
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 at 20:48
Don't use
then
when you could useawait
instead!– Bergi
Nov 18 at 20:49