0

edit: maybe I have simplified my example too much. Let me try again

file1.js
import conditonFunction from './conditonFunction'
console.log(conditonFunction()) 


file2.js
import asyncFunction from './asyncFunction'


export const conditonFunction = () => {
  if (condition) {
 return 'this doesnt matter'
  } else {
    asyncFunction().then(res => {
      return res[0].whatever
    })
  }
}

if my condition is not met, I want the logged value of conditonFunction to be the value of res inside of asyncFunction

What am I missing?

Christopher Mellor
  • 470
  • 2
  • 9
  • 23
  • Are you really calling `asyncFunction()` twice? Please also fix the syntax error - the `then(` call is missing a closing parenthesis. – Bergi Oct 24 '18 at 19:21

2 Answers2

1

An alternative is using async functions.

function asyncFunction() {
   return new Promise(function(resolve, reject) {
      setTimeout(function() {
        resolve(7);
      }, 2000);
   });
}

async function main() {
  var res = await asyncFunction(); 
  console.log(res);
}

main();
Wait for 2 seconds...
Ele
  • 33,468
  • 7
  • 37
  • 75
0

You seem to be looking for

asyncFunction().then(res => {
    return res[0].whatever;
}).then(console.log);

or simply

asyncFunction().then(res => {
    console.log(res[0].whatever);
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375