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










share|improve this question























  • 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














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










share|improve this question























  • 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












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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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






share|improve this answer






















  • 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 the result array in the same order as the comments 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










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',
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%2f53240675%2fhow-to-resolve-a-promise-in-an-async-function%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








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






share|improve this answer






















  • 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 the result array in the same order as the comments 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














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






share|improve this answer






















  • 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 the result array in the same order as the comments 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












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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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 the result array in the same order as the comments 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










  • @puskar now it does :)
    – Jonas Wilms
    Nov 10 at 16:06






  • 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







  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線