How to resolve a promise in an async function?
up vote
3
down vote
favorite
I have a lifecycle method componentDidMount that calls upon a recursive async method and I want the recursive function to return a promise after all the data has been fetched.
async componentDidMount()
let response = await fetch(`$STORY_URL$this.props.match.params.id.json`);
let result = await response.json();
totalComments = result.descendants;
await this.fetchComments(result.kids, MARGIN);
this.setState(
by: result.by,
time: calculateTimeDifference(result.time)
);
and the function being called is
async fetchComments(comment, margin)
return new Promise((resolve, reject) =>
comment.map(async commentId =>
let response = await fetch(`$STORY_URL$commentId.json`);
let result = await response.json();
comments.push(
by: result.by,
margin: margin
);
if (comments.length === totalComments + 1) resolve();
if (result.kids !== undefined) this.fetchComments(result.kids, margin * 2);
);
);
but the resolve method is not returning back to the componentDidMount before the setState. I checked by console logging. I don't know what I am doing wrong
javascript reactjs async-await es6-promise
add a comment |
up vote
3
down vote
favorite
I have a lifecycle method componentDidMount that calls upon a recursive async method and I want the recursive function to return a promise after all the data has been fetched.
async componentDidMount()
let response = await fetch(`$STORY_URL$this.props.match.params.id.json`);
let result = await response.json();
totalComments = result.descendants;
await this.fetchComments(result.kids, MARGIN);
this.setState(
by: result.by,
time: calculateTimeDifference(result.time)
);
and the function being called is
async fetchComments(comment, margin)
return new Promise((resolve, reject) =>
comment.map(async commentId =>
let response = await fetch(`$STORY_URL$commentId.json`);
let result = await response.json();
comments.push(
by: result.by,
margin: margin
);
if (comments.length === totalComments + 1) resolve();
if (result.kids !== undefined) this.fetchComments(result.kids, margin * 2);
);
);
but the resolve method is not returning back to the componentDidMount before the setState. I checked by console logging. I don't know what I am doing wrong
javascript reactjs async-await es6-promise
fetch
returns a promise. so you may not need to put fetch inside a promise
– brk
Nov 10 at 15:57
how can i resolve use that promise to resolve await?
– PuskarShestha
Nov 10 at 16:02
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a lifecycle method componentDidMount that calls upon a recursive async method and I want the recursive function to return a promise after all the data has been fetched.
async componentDidMount()
let response = await fetch(`$STORY_URL$this.props.match.params.id.json`);
let result = await response.json();
totalComments = result.descendants;
await this.fetchComments(result.kids, MARGIN);
this.setState(
by: result.by,
time: calculateTimeDifference(result.time)
);
and the function being called is
async fetchComments(comment, margin)
return new Promise((resolve, reject) =>
comment.map(async commentId =>
let response = await fetch(`$STORY_URL$commentId.json`);
let result = await response.json();
comments.push(
by: result.by,
margin: margin
);
if (comments.length === totalComments + 1) resolve();
if (result.kids !== undefined) this.fetchComments(result.kids, margin * 2);
);
);
but the resolve method is not returning back to the componentDidMount before the setState. I checked by console logging. I don't know what I am doing wrong
javascript reactjs async-await es6-promise
I have a lifecycle method componentDidMount that calls upon a recursive async method and I want the recursive function to return a promise after all the data has been fetched.
async componentDidMount()
let response = await fetch(`$STORY_URL$this.props.match.params.id.json`);
let result = await response.json();
totalComments = result.descendants;
await this.fetchComments(result.kids, MARGIN);
this.setState(
by: result.by,
time: calculateTimeDifference(result.time)
);
and the function being called is
async fetchComments(comment, margin)
return new Promise((resolve, reject) =>
comment.map(async commentId =>
let response = await fetch(`$STORY_URL$commentId.json`);
let result = await response.json();
comments.push(
by: result.by,
margin: margin
);
if (comments.length === totalComments + 1) resolve();
if (result.kids !== undefined) this.fetchComments(result.kids, margin * 2);
);
);
but the resolve method is not returning back to the componentDidMount before the setState. I checked by console logging. I don't know what I am doing wrong
javascript reactjs async-await es6-promise
javascript reactjs async-await es6-promise
edited Nov 10 at 16:52
lgwilliams
311313
311313
asked Nov 10 at 15:55
PuskarShestha
10610
10610
fetch
returns a promise. so you may not need to put fetch inside a promise
– brk
Nov 10 at 15:57
how can i resolve use that promise to resolve await?
– PuskarShestha
Nov 10 at 16:02
add a comment |
fetch
returns a promise. so you may not need to put fetch inside a promise
– brk
Nov 10 at 15:57
how can i resolve use that promise to resolve await?
– PuskarShestha
Nov 10 at 16:02
fetch
returns a promise. so you may not need to put fetch inside a promise– brk
Nov 10 at 15:57
fetch
returns a promise. so you may not need to put fetch inside a promise– brk
Nov 10 at 15:57
how can i resolve use that promise to resolve await?
– PuskarShestha
Nov 10 at 16:02
how can i resolve use that promise to resolve await?
– PuskarShestha
Nov 10 at 16:02
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
You are overcomplicating things. Use Promise.all
:
async fetchComments(comments, margin)
// Collect all the results here, otgerwise we would have to flatMap the promises which is more complicated
const result = ;
// Make sure that all comments were processed before returning
await Promise.all( comments.map(async commentId =>
const response = await fetch(`$STORY_URL$commentId.json`);
const kids, by = await response.json();
if(kids)
// Get all children and append them to the results, right after the children
const children = await fetchComments(kids, margin * 2);
result.push( by, margin , ...children);
else
// Otherwise just append this node
result.push( by, margin );
));
return result;
If the order matters, you have to flatten the result of Promise.all
:
async fetchComments(comments, margin)
const result = await Promise.all(comments.map( async commentID =>
const response = await fetch(STORY_URL + commentID + ".json");
const by, kids = await response.json();
const result = [ by, margin ];
if(kids) result.push(... await fetchComments(kids, margin * 2));
return result;
));
// Flatten the results
return .concat(...result);
the goal of this function is to push all the data into an array. Will this do the task?
– PuskarShestha
Nov 10 at 16:06
@puskar now it does :)
– Jonas Wilms
Nov 10 at 16:06
1
This won't necessary push things into theresult
array in the same order as thecomments
were if that matters.
– jfriend00
Nov 10 at 16:08
1
@puskar yes that is possible, but its slightly more complicated.
– Jonas Wilms
Nov 10 at 16:22
1
Thanks Jonas. It works like a charm.
– PuskarShestha
Nov 10 at 16:28
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You are overcomplicating things. Use Promise.all
:
async fetchComments(comments, margin)
// Collect all the results here, otgerwise we would have to flatMap the promises which is more complicated
const result = ;
// Make sure that all comments were processed before returning
await Promise.all( comments.map(async commentId =>
const response = await fetch(`$STORY_URL$commentId.json`);
const kids, by = await response.json();
if(kids)
// Get all children and append them to the results, right after the children
const children = await fetchComments(kids, margin * 2);
result.push( by, margin , ...children);
else
// Otherwise just append this node
result.push( by, margin );
));
return result;
If the order matters, you have to flatten the result of Promise.all
:
async fetchComments(comments, margin)
const result = await Promise.all(comments.map( async commentID =>
const response = await fetch(STORY_URL + commentID + ".json");
const by, kids = await response.json();
const result = [ by, margin ];
if(kids) result.push(... await fetchComments(kids, margin * 2));
return result;
));
// Flatten the results
return .concat(...result);
the goal of this function is to push all the data into an array. Will this do the task?
– PuskarShestha
Nov 10 at 16:06
@puskar now it does :)
– Jonas Wilms
Nov 10 at 16:06
1
This won't necessary push things into theresult
array in the same order as thecomments
were if that matters.
– jfriend00
Nov 10 at 16:08
1
@puskar yes that is possible, but its slightly more complicated.
– Jonas Wilms
Nov 10 at 16:22
1
Thanks Jonas. It works like a charm.
– PuskarShestha
Nov 10 at 16:28
|
show 2 more comments
up vote
3
down vote
accepted
You are overcomplicating things. Use Promise.all
:
async fetchComments(comments, margin)
// Collect all the results here, otgerwise we would have to flatMap the promises which is more complicated
const result = ;
// Make sure that all comments were processed before returning
await Promise.all( comments.map(async commentId =>
const response = await fetch(`$STORY_URL$commentId.json`);
const kids, by = await response.json();
if(kids)
// Get all children and append them to the results, right after the children
const children = await fetchComments(kids, margin * 2);
result.push( by, margin , ...children);
else
// Otherwise just append this node
result.push( by, margin );
));
return result;
If the order matters, you have to flatten the result of Promise.all
:
async fetchComments(comments, margin)
const result = await Promise.all(comments.map( async commentID =>
const response = await fetch(STORY_URL + commentID + ".json");
const by, kids = await response.json();
const result = [ by, margin ];
if(kids) result.push(... await fetchComments(kids, margin * 2));
return result;
));
// Flatten the results
return .concat(...result);
the goal of this function is to push all the data into an array. Will this do the task?
– PuskarShestha
Nov 10 at 16:06
@puskar now it does :)
– Jonas Wilms
Nov 10 at 16:06
1
This won't necessary push things into theresult
array in the same order as thecomments
were if that matters.
– jfriend00
Nov 10 at 16:08
1
@puskar yes that is possible, but its slightly more complicated.
– Jonas Wilms
Nov 10 at 16:22
1
Thanks Jonas. It works like a charm.
– PuskarShestha
Nov 10 at 16:28
|
show 2 more comments
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You are overcomplicating things. Use Promise.all
:
async fetchComments(comments, margin)
// Collect all the results here, otgerwise we would have to flatMap the promises which is more complicated
const result = ;
// Make sure that all comments were processed before returning
await Promise.all( comments.map(async commentId =>
const response = await fetch(`$STORY_URL$commentId.json`);
const kids, by = await response.json();
if(kids)
// Get all children and append them to the results, right after the children
const children = await fetchComments(kids, margin * 2);
result.push( by, margin , ...children);
else
// Otherwise just append this node
result.push( by, margin );
));
return result;
If the order matters, you have to flatten the result of Promise.all
:
async fetchComments(comments, margin)
const result = await Promise.all(comments.map( async commentID =>
const response = await fetch(STORY_URL + commentID + ".json");
const by, kids = await response.json();
const result = [ by, margin ];
if(kids) result.push(... await fetchComments(kids, margin * 2));
return result;
));
// Flatten the results
return .concat(...result);
You are overcomplicating things. Use Promise.all
:
async fetchComments(comments, margin)
// Collect all the results here, otgerwise we would have to flatMap the promises which is more complicated
const result = ;
// Make sure that all comments were processed before returning
await Promise.all( comments.map(async commentId =>
const response = await fetch(`$STORY_URL$commentId.json`);
const kids, by = await response.json();
if(kids)
// Get all children and append them to the results, right after the children
const children = await fetchComments(kids, margin * 2);
result.push( by, margin , ...children);
else
// Otherwise just append this node
result.push( by, margin );
));
return result;
If the order matters, you have to flatten the result of Promise.all
:
async fetchComments(comments, margin)
const result = await Promise.all(comments.map( async commentID =>
const response = await fetch(STORY_URL + commentID + ".json");
const by, kids = await response.json();
const result = [ by, margin ];
if(kids) result.push(... await fetchComments(kids, margin * 2));
return result;
));
// Flatten the results
return .concat(...result);
edited Nov 10 at 16:21
answered Nov 10 at 16:01
Jonas Wilms
52.2k42445
52.2k42445
the goal of this function is to push all the data into an array. Will this do the task?
– PuskarShestha
Nov 10 at 16:06
@puskar now it does :)
– Jonas Wilms
Nov 10 at 16:06
1
This won't necessary push things into theresult
array in the same order as thecomments
were if that matters.
– jfriend00
Nov 10 at 16:08
1
@puskar yes that is possible, but its slightly more complicated.
– Jonas Wilms
Nov 10 at 16:22
1
Thanks Jonas. It works like a charm.
– PuskarShestha
Nov 10 at 16:28
|
show 2 more comments
the goal of this function is to push all the data into an array. Will this do the task?
– PuskarShestha
Nov 10 at 16:06
@puskar now it does :)
– Jonas Wilms
Nov 10 at 16:06
1
This won't necessary push things into theresult
array in the same order as thecomments
were if that matters.
– jfriend00
Nov 10 at 16:08
1
@puskar yes that is possible, but its slightly more complicated.
– Jonas Wilms
Nov 10 at 16:22
1
Thanks Jonas. It works like a charm.
– PuskarShestha
Nov 10 at 16:28
the goal of this function is to push all the data into an array. Will this do the task?
– PuskarShestha
Nov 10 at 16:06
the goal of this function is to push all the data into an array. Will this do the task?
– PuskarShestha
Nov 10 at 16:06
@puskar now it does :)
– Jonas Wilms
Nov 10 at 16:06
@puskar now it does :)
– Jonas Wilms
Nov 10 at 16:06
1
1
This won't necessary push things into the
result
array in the same order as the comments
were if that matters.– jfriend00
Nov 10 at 16:08
This won't necessary push things into the
result
array in the same order as the comments
were if that matters.– jfriend00
Nov 10 at 16:08
1
1
@puskar yes that is possible, but its slightly more complicated.
– Jonas Wilms
Nov 10 at 16:22
@puskar yes that is possible, but its slightly more complicated.
– Jonas Wilms
Nov 10 at 16:22
1
1
Thanks Jonas. It works like a charm.
– PuskarShestha
Nov 10 at 16:28
Thanks Jonas. It works like a charm.
– PuskarShestha
Nov 10 at 16:28
|
show 2 more comments
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%2f53240675%2fhow-to-resolve-a-promise-in-an-async-function%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
fetch
returns a promise. so you may not need to put fetch inside a promise– brk
Nov 10 at 15:57
how can i resolve use that promise to resolve await?
– PuskarShestha
Nov 10 at 16:02