'Nested async-await while using node js
I have been stuck while trying to implement the following folder/code structure using nested async-await routines in Node.JS.
Seems this approach is creating some problems for Node.JS and the execution seems to fail and calling arbitrary routines.
//main.js
const main = async () => {
let data = await foo(a, b)
//Continue processing
};
//foo.js
const foo = async (a, b) => {
const x = a - 10; const y = b + 10; //do some arbitrary data manipulation here
let data = await bar(x, y);
return data;
};
//bar.js
const bar = async (c, d) => {
let data = await c.save(); //assume c is a mongoose object (eg; user)
return data;
};
The above code does not return the saved record from the db. Is a nested async-await supported in Node.JS?
I do significant pre & post processing in both of the called routines.
Solution 1:[1]
Caused by memory leaks, I believe. Declaring a number of variables potentially caused heap/stack? issues that caused the chaotic behavior. Putting all variables into a single object solved the issue, this time. Interesting find!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
