Multiple requests with axios without waiting for all of them to finish in an array list?
up vote
0
down vote
favorite
taskList.push(
const data =
url: 'http://$requestUrl?$argsString',
headers:
'Content-Type': 'application/octet-stream',
Authorization: signature //,
//'Content-Length': buffer.length
,
method: 'POST',
data: buffer
return axios(data)
)
try
const data = await Promise.all(taskList)
const res = data.map(d => d.data)
console.log(res)
//ctx.state.data = res
catch (e)
console.log(e)
throw e
How to do not wait all requests finish, because all finish needs too long time. If any request finish, I print it out, it will be very fast for users.
javascript node.js axios
add a comment |
up vote
0
down vote
favorite
taskList.push(
const data =
url: 'http://$requestUrl?$argsString',
headers:
'Content-Type': 'application/octet-stream',
Authorization: signature //,
//'Content-Length': buffer.length
,
method: 'POST',
data: buffer
return axios(data)
)
try
const data = await Promise.all(taskList)
const res = data.map(d => d.data)
console.log(res)
//ctx.state.data = res
catch (e)
console.log(e)
throw e
How to do not wait all requests finish, because all finish needs too long time. If any request finish, I print it out, it will be very fast for users.
javascript node.js axios
2
No need to use Promise.all() , just add athen()to each request
– charlietfl
Nov 11 at 1:18
1
I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their ownthen()will make them independent.
– Akhil Gautam
Nov 11 at 1:23
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
taskList.push(
const data =
url: 'http://$requestUrl?$argsString',
headers:
'Content-Type': 'application/octet-stream',
Authorization: signature //,
//'Content-Length': buffer.length
,
method: 'POST',
data: buffer
return axios(data)
)
try
const data = await Promise.all(taskList)
const res = data.map(d => d.data)
console.log(res)
//ctx.state.data = res
catch (e)
console.log(e)
throw e
How to do not wait all requests finish, because all finish needs too long time. If any request finish, I print it out, it will be very fast for users.
javascript node.js axios
taskList.push(
const data =
url: 'http://$requestUrl?$argsString',
headers:
'Content-Type': 'application/octet-stream',
Authorization: signature //,
//'Content-Length': buffer.length
,
method: 'POST',
data: buffer
return axios(data)
)
try
const data = await Promise.all(taskList)
const res = data.map(d => d.data)
console.log(res)
//ctx.state.data = res
catch (e)
console.log(e)
throw e
How to do not wait all requests finish, because all finish needs too long time. If any request finish, I print it out, it will be very fast for users.
javascript node.js axios
javascript node.js axios
asked Nov 11 at 1:14
Gank
3,58433739
3,58433739
2
No need to use Promise.all() , just add athen()to each request
– charlietfl
Nov 11 at 1:18
1
I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their ownthen()will make them independent.
– Akhil Gautam
Nov 11 at 1:23
add a comment |
2
No need to use Promise.all() , just add athen()to each request
– charlietfl
Nov 11 at 1:18
1
I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their ownthen()will make them independent.
– Akhil Gautam
Nov 11 at 1:23
2
2
No need to use Promise.all() , just add a
then() to each request– charlietfl
Nov 11 at 1:18
No need to use Promise.all() , just add a
then() to each request– charlietfl
Nov 11 at 1:18
1
1
I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their own
then() will make them independent.– Akhil Gautam
Nov 11 at 1:23
I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their own
then() will make them independent.– Akhil Gautam
Nov 11 at 1:23
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
To run each Promise just add a .then() to each of one them or to the Promise.all, however you will not be able to .map the data after:
try {
const data = Promise.all(taskList).then(() => console.log('finished'));
// this will not wait to run, so data will be probably undefined
const res = data.map(d => d.data)
...
So I'd suggest following the pattern and waiting for them to finish.
If it's taking too long maybe you should try improve the performance of the response in the back-end if that's possible.
TypeError: data.map is not a functionBecause, data has no value yet
– Gank
Nov 11 at 7:20
add a comment |
up vote
1
down vote
In case each response should be logged as soon as it's completed, it should be:
const responses = await Promise.all(taskList.map(async task =>
const data = await task;
console.log(data);
return data;
));
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
To run each Promise just add a .then() to each of one them or to the Promise.all, however you will not be able to .map the data after:
try {
const data = Promise.all(taskList).then(() => console.log('finished'));
// this will not wait to run, so data will be probably undefined
const res = data.map(d => d.data)
...
So I'd suggest following the pattern and waiting for them to finish.
If it's taking too long maybe you should try improve the performance of the response in the back-end if that's possible.
TypeError: data.map is not a functionBecause, data has no value yet
– Gank
Nov 11 at 7:20
add a comment |
up vote
1
down vote
To run each Promise just add a .then() to each of one them or to the Promise.all, however you will not be able to .map the data after:
try {
const data = Promise.all(taskList).then(() => console.log('finished'));
// this will not wait to run, so data will be probably undefined
const res = data.map(d => d.data)
...
So I'd suggest following the pattern and waiting for them to finish.
If it's taking too long maybe you should try improve the performance of the response in the back-end if that's possible.
TypeError: data.map is not a functionBecause, data has no value yet
– Gank
Nov 11 at 7:20
add a comment |
up vote
1
down vote
up vote
1
down vote
To run each Promise just add a .then() to each of one them or to the Promise.all, however you will not be able to .map the data after:
try {
const data = Promise.all(taskList).then(() => console.log('finished'));
// this will not wait to run, so data will be probably undefined
const res = data.map(d => d.data)
...
So I'd suggest following the pattern and waiting for them to finish.
If it's taking too long maybe you should try improve the performance of the response in the back-end if that's possible.
To run each Promise just add a .then() to each of one them or to the Promise.all, however you will not be able to .map the data after:
try {
const data = Promise.all(taskList).then(() => console.log('finished'));
// this will not wait to run, so data will be probably undefined
const res = data.map(d => d.data)
...
So I'd suggest following the pattern and waiting for them to finish.
If it's taking too long maybe you should try improve the performance of the response in the back-end if that's possible.
answered Nov 11 at 5:54
sudodev
11919
11919
TypeError: data.map is not a functionBecause, data has no value yet
– Gank
Nov 11 at 7:20
add a comment |
TypeError: data.map is not a functionBecause, data has no value yet
– Gank
Nov 11 at 7:20
TypeError: data.map is not a function Because, data has no value yet– Gank
Nov 11 at 7:20
TypeError: data.map is not a function Because, data has no value yet– Gank
Nov 11 at 7:20
add a comment |
up vote
1
down vote
In case each response should be logged as soon as it's completed, it should be:
const responses = await Promise.all(taskList.map(async task =>
const data = await task;
console.log(data);
return data;
));
add a comment |
up vote
1
down vote
In case each response should be logged as soon as it's completed, it should be:
const responses = await Promise.all(taskList.map(async task =>
const data = await task;
console.log(data);
return data;
));
add a comment |
up vote
1
down vote
up vote
1
down vote
In case each response should be logged as soon as it's completed, it should be:
const responses = await Promise.all(taskList.map(async task =>
const data = await task;
console.log(data);
return data;
));
In case each response should be logged as soon as it's completed, it should be:
const responses = await Promise.all(taskList.map(async task =>
const data = await task;
console.log(data);
return data;
));
answered Nov 11 at 7:53
estus
63.7k2193201
63.7k2193201
add a comment |
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%2f53244989%2fmultiple-requests-with-axios-without-waiting-for-all-of-them-to-finish-in-an-arr%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
2
No need to use Promise.all() , just add a
then()to each request– charlietfl
Nov 11 at 1:18
1
I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their own
then()will make them independent.– Akhil Gautam
Nov 11 at 1:23