I am having a bit of a problem with my code(Node.js). I want have 2 files app.js and state.js, i basically created the two to try and solve the issue i am having.
on the second file "State.js" i have a function that returns an object and i delayed the return by 10 secs (With impression of a slow internet, etc). and i am trying to retrieve the object in the app.js file. but at the moment, it doesn't work. I have tried using promise (async & await) but that returns an error on the terminal, i also tried running a setInterval to monitor the variable i which to use in retrieving the object to only execute when it has data but that doesn't work as well. Please I need assistance as soon as possible.
Below is the code examples for the 2 scenario.
=========================== state.js file =======================
function test(val){
setTimeout(() => {
return {
code: 0,
value: val
}
}, 10000);
}
module.exports = test;
========================== app.js file (Using the promise approach)============================
async function runNow(){
let v = await t('some string');
console.log(v.value)
}
runNow();
================================== app.js (using the interval approach) ==========
function runNow(){
let engine = setInterval(() => {
let v = t('some string');
if(v === null || v === undefined){
console.log(v);
}else{
clearInterval(engine);
console.log(v.value);
}
}, 5000);
// console.log(v.value);
}
runNow();