How do I export a node.js module which is inside the Promise.all (somefunction) . then (function (){})
Asked
Active
Viewed 58 times
1 Answers
0
All you can do is export the promise and wait for it in the other file:
// index1.js
…
module.exports = Promise.all(ted);
// index2.js
require('./index1').then(getStatus => {
console.log(getStatus);
…
});

Bergi
- 630,263
- 148
- 957
- 1,375
-
Sorry, typo. You need to refer to the callback parameter of course. – Bergi May 11 '17 at 09:07
-
Add a timer where? There's no place you could add a timer to my code that would make it behave differently. – Bergi May 11 '17 at 09:12
-
Yes. You need to place all code that should wait for `index1.js` inside the `then` callback. – Bergi May 11 '17 at 09:45
-
your index2 does `require` itself instead of index1? – Bergi May 11 '17 at 19:32
-
No idea. Maybe because you're calling `db.close()` before the thing is found? Also I'm not sure what your operation in index1 is (you didn't post that code), or why you're sure that the problem comes from executing `p()` while the insertion hasn't finished yet. – Bergi May 11 '17 at 21:39
-
But did the promise actually resolve at the right time, and was *that* promise awaited? – Bergi May 11 '17 at 23:22
-
Yeah, as I expected - you are calling `savemongo(myobj,str)` but there's no attempt to wait for it to finish. Also, you `resolve` your `ted` promises regardless what the status of the distance matrix in that callback is. – Bergi May 12 '17 at 00:04
-
Oh yea Even i realised that . Thank you @bergi Appreciate your help.. thank you :) – karthik006 May 12 '17 at 00:21
-
You might want to [ask a new question](http://stackoverflow.com/questions/ask) about that, providing the relevant part of the code. – Bergi May 12 '17 at 01:18
-
Because I thought promise.all waits for all the async operation to finish – karthik006 May 12 '17 at 01:28
-
All async operations whose promises it was passed (in fact, it doesn't even know about the operations, it only waits for the promises to settle). Not all async operations arbitrarily occuring anywhere. – Bergi May 12 '17 at 03:07
-
Thank you .I have asked a new question – karthik006 May 12 '17 at 03:08
-
http://stackoverflow.com/questions/43928459/how-do-i-nest-a-promise-inside-another-promise-function-in-node-js – karthik006 May 12 '17 at 03:08