I have a subreddit where I want to parse the ments of all the daily threads. To do so, I have to get the thread ids. I am using snoowrap as an api wrapper.
I am having a hard time wrapping my head around the asynchronous nature of node. When I call subreddit.search({query: 'Daily General Discussion - ' + date})
, I get a Promise.
I then resolve the promise to get the thread id and push it to a new array (I tried saving the value with arr[i] = a[0].id
, but only the last result was getting stored in my array, suggesting that Promise.resolve gets called after my for loop).
How do I stop the rest of my code from executing before the Promise is resolved? Using setTimeout seems inefficient and unreliable since the resolve time can be longer than my setTimeout time.
The code below works, I just want to avoid using setTimeout.
const subreddit = reddit.getSubreddit('subreddit')
let numberOfDaysBack = 50;
let arrIds = new Array(0);
for (var i = 0; i < numberOfDaysBack; i++) {
let date = moment().subtract(0+i, 'days').format('MMMM D, YYYY')
let dailyThread = subreddit.search({query: 'Daily General Discussion - ' + date})
Promise.resolve(dailyThread).then(a=>{
arrIds.push(a[0].id)
})
}
setTimeout(function(){
console.log(arrIds)
},1000)
doOtherStuff(arrIds)
I have a subreddit where I want to parse the ments of all the daily threads. To do so, I have to get the thread ids. I am using snoowrap as an api wrapper.
I am having a hard time wrapping my head around the asynchronous nature of node. When I call subreddit.search({query: 'Daily General Discussion - ' + date})
, I get a Promise.
I then resolve the promise to get the thread id and push it to a new array (I tried saving the value with arr[i] = a[0].id
, but only the last result was getting stored in my array, suggesting that Promise.resolve gets called after my for loop).
How do I stop the rest of my code from executing before the Promise is resolved? Using setTimeout seems inefficient and unreliable since the resolve time can be longer than my setTimeout time.
The code below works, I just want to avoid using setTimeout.
const subreddit = reddit.getSubreddit('subreddit')
let numberOfDaysBack = 50;
let arrIds = new Array(0);
for (var i = 0; i < numberOfDaysBack; i++) {
let date = moment().subtract(0+i, 'days').format('MMMM D, YYYY')
let dailyThread = subreddit.search({query: 'Daily General Discussion - ' + date})
Promise.resolve(dailyThread).then(a=>{
arrIds.push(a[0].id)
})
}
setTimeout(function(){
console.log(arrIds)
},1000)
doOtherStuff(arrIds)
Share
Improve this question
edited Oct 20, 2017 at 4:21
Huy
asked Oct 20, 2017 at 4:13
HuyHuy
11.2k14 gold badges59 silver badges102 bronze badges
4
-
@PatrickEvans Can you explicitly spell it out with me with my example? I am currently using
Promise.resolve().then()
– Huy Commented Oct 20, 2017 at 4:17 -
The code below works, I just want to avoid using setTimeout.
- wait for the promises to resolve – Jaromanda X Commented Oct 20, 2017 at 4:20 - @JaromandaX But if I want to use the values in the array for more putation, how do I ensure that the promises are resolved before I execute more code? – Huy Commented Oct 20, 2017 at 4:21
-
1
you wait for the promises to resolve using
Promise.all(promises).then(results => { here the results are yours to play with })
– Jaromanda X Commented Oct 20, 2017 at 4:23
4 Answers
Reset to default 5Map the promises into an array and then use Promise.all()
to run them concurrently. You will get the results in then
after all the promises are resolved.
const subreddit = reddit.getSubreddit('subreddit')
let numberOfDaysBack = 50
let arrIds
let promises = Array(numberOfDaysBack)
.fill()
.map((_, i) => {
let date = moment().subtract(0+i, 'days').format('MMMM D, YYYY')
let dailyThread = subreddit.search({query: 'Daily General Discussion - ' + date})
return dailyThread
})
Promise
.all(promises)
.then(results => {
arrIds = results.map(result => result.id)
doOtherStuff(arrIds)
});
Store all of your promises in an array, and then use Promise.all
:
const subreddit = reddit.getSubreddit('subreddit')
let numberOfDaysBack = 50;
let idPromises = [];
for (var i = 0; i < numberOfDaysBack; i++) {
let date = moment().subtract(i, 'days').format('MMMM D, YYYY')
let dailyThread = subreddit.search({query: 'Daily General Discussion - ' + date})
idPromises.push(dailyThread.then(a => a[0].id));
}
Promise.all(idPromises).then(arrIds => {
console.log(arrIds)
});
You can use Promise.all() in this type of use case where you have to create a list of promises and wait for all of them to finish.
const subreddit = reddit.getSubreddit('subreddit');
const numberOfDaysBack = 50;
const arrIds = new Array(0);
const promises = [];
for (var i = 0; i < numberOfDaysBack; i++) {
const date = moment().subtract(0+i, 'days').format('MMMM D, YYYY');
const dailyThread = subreddit.search({query: 'Daily General Discussion - ' + date});
promises.push(dailyThread);
}
Promise.all(promises).then(function(results){
results.forEach(function(a){
arrayIds.push(a[0].id);
});
console.log(arrayIds);
});
Assuming subreddit.search
returns a Promise
const subreddit = reddit.getSubreddit('subreddit')
let numberOfDaysBack = 50;
Promise.all(Array.fill(numberOfDaysBack).map((unused, i) => {
let date = moment().subtract(0+i, 'days').format('MMMM D, YYYY')
return subreddit.search({query: 'Daily General Discussion - ' + date}).then(a => a[0].id);
})).then(results => {
// here results is the array of "a[0].id"
});
// out here, don't expect to access results without using kludges
// so do it all in the `.then`