How do I wait for a promise in loop to finish before do some other stuff?In Node.js, how do I “include” functions from my other files?How do I convert an existing callback API to promises?How do I access previous promise results in a .then() chain?Wait until all ES6 promises complete, even rejected promisesHow to solve promise issue?How to Control Promise Chaining FlowJavascript Run Async Code For Each Item Of For Loop With PromisesRequest data to global variableNode JS Promise TypeError: Cannot read property 'then' of undefinedPromises - How to make asynchronous code execute synchronous without async / await?
How to preserve a rare version of a book?
Why increasing of the temperature of the objects like wood, paper etc. doesn't fire them?
How does one write a Right-to-Left ellipsis in Pages?
Can an earth elemental drag a tiny creature underground with Earth Glide?
TIP120 Transistor + Solenoid Failing Randomly
As a GM, is it bad form to ask for a moment to think when improvising?
Class Not Passing SObject By Reference
How to speed up large double sums in a table?
My large rocket is still flipping over
What are the requirements for a river delta to form?
What does the coin flipping before dying mean?
Installing Debian 10, upgrade to stable later?
What do you call a painting painted on a wall?
Primes in a Diamond
What is the thing used to help pouring liquids called?
What is a common way to tell if an academic is "above average," or outstanding in their field? Is their h-index (Hirsh index) one of them?
How to replace space with '+' symbol in a triangular array?
How do I, as a DM, handle a party that decides to set up an ambush in a dungeon?
Game artist computer workstation set-up – is this overkill?
What is a precise issue with allowing getters?
Has the United States ever had a non-Christian President?
What is more safe for browsing the web: PC or smartphone?
Which version of the Squat Nimbleness feat is correct?
Gerrymandering Puzzle - Rig the Election
How do I wait for a promise in loop to finish before do some other stuff?
In Node.js, how do I “include” functions from my other files?How do I convert an existing callback API to promises?How do I access previous promise results in a .then() chain?Wait until all ES6 promises complete, even rejected promisesHow to solve promise issue?How to Control Promise Chaining FlowJavascript Run Async Code For Each Item Of For Loop With PromisesRequest data to global variableNode JS Promise TypeError: Cannot read property 'then' of undefinedPromises - How to make asynchronous code execute synchronous without async / await?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I still confused about how to use promises. I have a for loop call an asynchronous method which returns a value. I use this value to push into an array. But when I print the array it is empty. Here is what I did:
async function getLink(link)
var browser = await puppeteer.launch(headless: true);
const page = await browser.newPage();
await page.goto(LINK)
const result = await page.evaluate( async() =>
let data = [];
const $ = window.$;
$('#gallery_01 .item').each(function(index, product)
data.push($(product).find('a').attr('data-image'));
);
return data;
);
await browser.close();
return result;
var final = [];
for (var i = 0; i < 10; i++)
var data = getLink(value[i].url).then(function(data)
console.log(data); // urls show here
final.push(data);
);
Promise.all(final).then(() =>
console.log(final) // empty
)
The final show empty. What did I do wrong with Promise? Pls help!
node.js promise
add a comment |
I still confused about how to use promises. I have a for loop call an asynchronous method which returns a value. I use this value to push into an array. But when I print the array it is empty. Here is what I did:
async function getLink(link)
var browser = await puppeteer.launch(headless: true);
const page = await browser.newPage();
await page.goto(LINK)
const result = await page.evaluate( async() =>
let data = [];
const $ = window.$;
$('#gallery_01 .item').each(function(index, product)
data.push($(product).find('a').attr('data-image'));
);
return data;
);
await browser.close();
return result;
var final = [];
for (var i = 0; i < 10; i++)
var data = getLink(value[i].url).then(function(data)
console.log(data); // urls show here
final.push(data);
);
Promise.all(final).then(() =>
console.log(final) // empty
)
The final show empty. What did I do wrong with Promise? Pls help!
node.js promise
is the page.goto() function supposed to take the link parameter instead of LINK? where is LINK defined?
– Shawn K
Mar 23 at 4:40
it isconstparameter. It work fine when I show thedata. Please see the edit
– tuanptit
Mar 23 at 6:03
Please be careful using Promise.all because it immediately (but always asynchronously) rejects when a single promise rejects with its rejected value. See the documentation for more information.
– lifeisfoo
Mar 23 at 7:09
add a comment |
I still confused about how to use promises. I have a for loop call an asynchronous method which returns a value. I use this value to push into an array. But when I print the array it is empty. Here is what I did:
async function getLink(link)
var browser = await puppeteer.launch(headless: true);
const page = await browser.newPage();
await page.goto(LINK)
const result = await page.evaluate( async() =>
let data = [];
const $ = window.$;
$('#gallery_01 .item').each(function(index, product)
data.push($(product).find('a').attr('data-image'));
);
return data;
);
await browser.close();
return result;
var final = [];
for (var i = 0; i < 10; i++)
var data = getLink(value[i].url).then(function(data)
console.log(data); // urls show here
final.push(data);
);
Promise.all(final).then(() =>
console.log(final) // empty
)
The final show empty. What did I do wrong with Promise? Pls help!
node.js promise
I still confused about how to use promises. I have a for loop call an asynchronous method which returns a value. I use this value to push into an array. But when I print the array it is empty. Here is what I did:
async function getLink(link)
var browser = await puppeteer.launch(headless: true);
const page = await browser.newPage();
await page.goto(LINK)
const result = await page.evaluate( async() =>
let data = [];
const $ = window.$;
$('#gallery_01 .item').each(function(index, product)
data.push($(product).find('a').attr('data-image'));
);
return data;
);
await browser.close();
return result;
var final = [];
for (var i = 0; i < 10; i++)
var data = getLink(value[i].url).then(function(data)
console.log(data); // urls show here
final.push(data);
);
Promise.all(final).then(() =>
console.log(final) // empty
)
The final show empty. What did I do wrong with Promise? Pls help!
node.js promise
node.js promise
edited Mar 23 at 6:23
Mark Meyer
42.6k33765
42.6k33765
asked Mar 23 at 4:31
tuanptittuanptit
53112
53112
is the page.goto() function supposed to take the link parameter instead of LINK? where is LINK defined?
– Shawn K
Mar 23 at 4:40
it isconstparameter. It work fine when I show thedata. Please see the edit
– tuanptit
Mar 23 at 6:03
Please be careful using Promise.all because it immediately (but always asynchronously) rejects when a single promise rejects with its rejected value. See the documentation for more information.
– lifeisfoo
Mar 23 at 7:09
add a comment |
is the page.goto() function supposed to take the link parameter instead of LINK? where is LINK defined?
– Shawn K
Mar 23 at 4:40
it isconstparameter. It work fine when I show thedata. Please see the edit
– tuanptit
Mar 23 at 6:03
Please be careful using Promise.all because it immediately (but always asynchronously) rejects when a single promise rejects with its rejected value. See the documentation for more information.
– lifeisfoo
Mar 23 at 7:09
is the page.goto() function supposed to take the link parameter instead of LINK? where is LINK defined?
– Shawn K
Mar 23 at 4:40
is the page.goto() function supposed to take the link parameter instead of LINK? where is LINK defined?
– Shawn K
Mar 23 at 4:40
it is
const parameter. It work fine when I show the data. Please see the edit– tuanptit
Mar 23 at 6:03
it is
const parameter. It work fine when I show the data. Please see the edit– tuanptit
Mar 23 at 6:03
Please be careful using Promise.all because it immediately (but always asynchronously) rejects when a single promise rejects with its rejected value. See the documentation for more information.
– lifeisfoo
Mar 23 at 7:09
Please be careful using Promise.all because it immediately (but always asynchronously) rejects when a single promise rejects with its rejected value. See the documentation for more information.
– lifeisfoo
Mar 23 at 7:09
add a comment |
2 Answers
2
active
oldest
votes
I can't see what value is, but it looks like it's supposed to be an array of objects with a url property?
Assuming the getLink() function is okay, try this for your loop:
const final = [];
for (var i = 0; i < 10; i++)
final.push(getLink(value[i].url));
Promise.all(final)
.then(data =>
console.log(data);
);
Or a slightly more compact way of accomplishing the same thing:
const promises = value.map(v => getLink(v.url));
Promise.all(promises)
.then(data =>
console.log(data);
);
@tuanptit Use Promise.all() carefully, it would throw an error at first instance a promise is rejected, and won't process the rest of promises.
– Kiran Mathew Mohan
Mar 23 at 8:00
add a comment |
Update: My bad, got a bit confused. The following code would only work without () => after the var fn
You are very close. Try this:
var final = [];
var results = []; // you need a separate array for results
for (var i = 0; i < 10; i++)
// renamed the variable, changed 'data' to 'fn'
var fn = () => getLink(value[i].url).then(function(data)
console.log(data); // urls show here
results.push(data);
);
final.push(fn);
Promise.all(final).then(() =>
console.log(results)
)
Promise.all accepts an array of promises. You have an array 'final' but seem to try to store the result of the fucntion execution as well as the function itself.
To do this correctly - first get an array of promises. Then pass them to Promise.all().
P.S. Assuming your function actually works, haven't looked at it, since the question was about promises.
You are pushing functions into the array not a promise.
– Mark Meyer
Mar 23 at 6:25
Still return empty
– tuanptit
Mar 23 at 6:30
I assumed the function returns a promise, because the function if async
– Andrii Rudavko
Mar 23 at 6:31
The function might return a promise, but not if you don't call it. In your codefinaljust contains a bunch of references to the functionfn
– Mark Meyer
Mar 23 at 6:32
1
@MarkMeyer You are correct. Thanks for pointing it out. Appreciate it)
– Andrii Rudavko
Mar 23 at 6:52
|
show 2 more comments
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',
autoActivateHeartbeat: false,
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
);
);
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%2f55310624%2fhow-do-i-wait-for-a-promise-in-loop-to-finish-before-do-some-other-stuff%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I can't see what value is, but it looks like it's supposed to be an array of objects with a url property?
Assuming the getLink() function is okay, try this for your loop:
const final = [];
for (var i = 0; i < 10; i++)
final.push(getLink(value[i].url));
Promise.all(final)
.then(data =>
console.log(data);
);
Or a slightly more compact way of accomplishing the same thing:
const promises = value.map(v => getLink(v.url));
Promise.all(promises)
.then(data =>
console.log(data);
);
@tuanptit Use Promise.all() carefully, it would throw an error at first instance a promise is rejected, and won't process the rest of promises.
– Kiran Mathew Mohan
Mar 23 at 8:00
add a comment |
I can't see what value is, but it looks like it's supposed to be an array of objects with a url property?
Assuming the getLink() function is okay, try this for your loop:
const final = [];
for (var i = 0; i < 10; i++)
final.push(getLink(value[i].url));
Promise.all(final)
.then(data =>
console.log(data);
);
Or a slightly more compact way of accomplishing the same thing:
const promises = value.map(v => getLink(v.url));
Promise.all(promises)
.then(data =>
console.log(data);
);
@tuanptit Use Promise.all() carefully, it would throw an error at first instance a promise is rejected, and won't process the rest of promises.
– Kiran Mathew Mohan
Mar 23 at 8:00
add a comment |
I can't see what value is, but it looks like it's supposed to be an array of objects with a url property?
Assuming the getLink() function is okay, try this for your loop:
const final = [];
for (var i = 0; i < 10; i++)
final.push(getLink(value[i].url));
Promise.all(final)
.then(data =>
console.log(data);
);
Or a slightly more compact way of accomplishing the same thing:
const promises = value.map(v => getLink(v.url));
Promise.all(promises)
.then(data =>
console.log(data);
);
I can't see what value is, but it looks like it's supposed to be an array of objects with a url property?
Assuming the getLink() function is okay, try this for your loop:
const final = [];
for (var i = 0; i < 10; i++)
final.push(getLink(value[i].url));
Promise.all(final)
.then(data =>
console.log(data);
);
Or a slightly more compact way of accomplishing the same thing:
const promises = value.map(v => getLink(v.url));
Promise.all(promises)
.then(data =>
console.log(data);
);
answered Mar 23 at 6:27
Ben ChamberlinBen Chamberlin
514212
514212
@tuanptit Use Promise.all() carefully, it would throw an error at first instance a promise is rejected, and won't process the rest of promises.
– Kiran Mathew Mohan
Mar 23 at 8:00
add a comment |
@tuanptit Use Promise.all() carefully, it would throw an error at first instance a promise is rejected, and won't process the rest of promises.
– Kiran Mathew Mohan
Mar 23 at 8:00
@tuanptit Use Promise.all() carefully, it would throw an error at first instance a promise is rejected, and won't process the rest of promises.
– Kiran Mathew Mohan
Mar 23 at 8:00
@tuanptit Use Promise.all() carefully, it would throw an error at first instance a promise is rejected, and won't process the rest of promises.
– Kiran Mathew Mohan
Mar 23 at 8:00
add a comment |
Update: My bad, got a bit confused. The following code would only work without () => after the var fn
You are very close. Try this:
var final = [];
var results = []; // you need a separate array for results
for (var i = 0; i < 10; i++)
// renamed the variable, changed 'data' to 'fn'
var fn = () => getLink(value[i].url).then(function(data)
console.log(data); // urls show here
results.push(data);
);
final.push(fn);
Promise.all(final).then(() =>
console.log(results)
)
Promise.all accepts an array of promises. You have an array 'final' but seem to try to store the result of the fucntion execution as well as the function itself.
To do this correctly - first get an array of promises. Then pass them to Promise.all().
P.S. Assuming your function actually works, haven't looked at it, since the question was about promises.
You are pushing functions into the array not a promise.
– Mark Meyer
Mar 23 at 6:25
Still return empty
– tuanptit
Mar 23 at 6:30
I assumed the function returns a promise, because the function if async
– Andrii Rudavko
Mar 23 at 6:31
The function might return a promise, but not if you don't call it. In your codefinaljust contains a bunch of references to the functionfn
– Mark Meyer
Mar 23 at 6:32
1
@MarkMeyer You are correct. Thanks for pointing it out. Appreciate it)
– Andrii Rudavko
Mar 23 at 6:52
|
show 2 more comments
Update: My bad, got a bit confused. The following code would only work without () => after the var fn
You are very close. Try this:
var final = [];
var results = []; // you need a separate array for results
for (var i = 0; i < 10; i++)
// renamed the variable, changed 'data' to 'fn'
var fn = () => getLink(value[i].url).then(function(data)
console.log(data); // urls show here
results.push(data);
);
final.push(fn);
Promise.all(final).then(() =>
console.log(results)
)
Promise.all accepts an array of promises. You have an array 'final' but seem to try to store the result of the fucntion execution as well as the function itself.
To do this correctly - first get an array of promises. Then pass them to Promise.all().
P.S. Assuming your function actually works, haven't looked at it, since the question was about promises.
You are pushing functions into the array not a promise.
– Mark Meyer
Mar 23 at 6:25
Still return empty
– tuanptit
Mar 23 at 6:30
I assumed the function returns a promise, because the function if async
– Andrii Rudavko
Mar 23 at 6:31
The function might return a promise, but not if you don't call it. In your codefinaljust contains a bunch of references to the functionfn
– Mark Meyer
Mar 23 at 6:32
1
@MarkMeyer You are correct. Thanks for pointing it out. Appreciate it)
– Andrii Rudavko
Mar 23 at 6:52
|
show 2 more comments
Update: My bad, got a bit confused. The following code would only work without () => after the var fn
You are very close. Try this:
var final = [];
var results = []; // you need a separate array for results
for (var i = 0; i < 10; i++)
// renamed the variable, changed 'data' to 'fn'
var fn = () => getLink(value[i].url).then(function(data)
console.log(data); // urls show here
results.push(data);
);
final.push(fn);
Promise.all(final).then(() =>
console.log(results)
)
Promise.all accepts an array of promises. You have an array 'final' but seem to try to store the result of the fucntion execution as well as the function itself.
To do this correctly - first get an array of promises. Then pass them to Promise.all().
P.S. Assuming your function actually works, haven't looked at it, since the question was about promises.
Update: My bad, got a bit confused. The following code would only work without () => after the var fn
You are very close. Try this:
var final = [];
var results = []; // you need a separate array for results
for (var i = 0; i < 10; i++)
// renamed the variable, changed 'data' to 'fn'
var fn = () => getLink(value[i].url).then(function(data)
console.log(data); // urls show here
results.push(data);
);
final.push(fn);
Promise.all(final).then(() =>
console.log(results)
)
Promise.all accepts an array of promises. You have an array 'final' but seem to try to store the result of the fucntion execution as well as the function itself.
To do this correctly - first get an array of promises. Then pass them to Promise.all().
P.S. Assuming your function actually works, haven't looked at it, since the question was about promises.
edited Mar 23 at 15:44
answered Mar 23 at 6:16
Andrii RudavkoAndrii Rudavko
1807
1807
You are pushing functions into the array not a promise.
– Mark Meyer
Mar 23 at 6:25
Still return empty
– tuanptit
Mar 23 at 6:30
I assumed the function returns a promise, because the function if async
– Andrii Rudavko
Mar 23 at 6:31
The function might return a promise, but not if you don't call it. In your codefinaljust contains a bunch of references to the functionfn
– Mark Meyer
Mar 23 at 6:32
1
@MarkMeyer You are correct. Thanks for pointing it out. Appreciate it)
– Andrii Rudavko
Mar 23 at 6:52
|
show 2 more comments
You are pushing functions into the array not a promise.
– Mark Meyer
Mar 23 at 6:25
Still return empty
– tuanptit
Mar 23 at 6:30
I assumed the function returns a promise, because the function if async
– Andrii Rudavko
Mar 23 at 6:31
The function might return a promise, but not if you don't call it. In your codefinaljust contains a bunch of references to the functionfn
– Mark Meyer
Mar 23 at 6:32
1
@MarkMeyer You are correct. Thanks for pointing it out. Appreciate it)
– Andrii Rudavko
Mar 23 at 6:52
You are pushing functions into the array not a promise.
– Mark Meyer
Mar 23 at 6:25
You are pushing functions into the array not a promise.
– Mark Meyer
Mar 23 at 6:25
Still return empty
– tuanptit
Mar 23 at 6:30
Still return empty
– tuanptit
Mar 23 at 6:30
I assumed the function returns a promise, because the function if async
– Andrii Rudavko
Mar 23 at 6:31
I assumed the function returns a promise, because the function if async
– Andrii Rudavko
Mar 23 at 6:31
The function might return a promise, but not if you don't call it. In your code
final just contains a bunch of references to the function fn– Mark Meyer
Mar 23 at 6:32
The function might return a promise, but not if you don't call it. In your code
final just contains a bunch of references to the function fn– Mark Meyer
Mar 23 at 6:32
1
1
@MarkMeyer You are correct. Thanks for pointing it out. Appreciate it)
– Andrii Rudavko
Mar 23 at 6:52
@MarkMeyer You are correct. Thanks for pointing it out. Appreciate it)
– Andrii Rudavko
Mar 23 at 6:52
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f55310624%2fhow-do-i-wait-for-a-promise-in-loop-to-finish-before-do-some-other-stuff%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
is the page.goto() function supposed to take the link parameter instead of LINK? where is LINK defined?
– Shawn K
Mar 23 at 4:40
it is
constparameter. It work fine when I show thedata. Please see the edit– tuanptit
Mar 23 at 6:03
Please be careful using Promise.all because it immediately (but always asynchronously) rejects when a single promise rejects with its rejected value. See the documentation for more information.
– lifeisfoo
Mar 23 at 7:09