0

編集: 例を単純化しすぎたのかもしれません。もう一度試してみましょう

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
    })
  }
}

条件が満たされない場合、ログに記録されconditonFunctionた値を のres内部の値にしたいasyncFunction

私は何が欠けていますか?

4

2 に答える 2

1

async別の方法は、関数を使用することです。

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...

于 2018-10-24T19:20:49.160 に答える
0

探しているようです

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

または単に

asyncFunction().then(res => {
    console.log(res[0].whatever);
});
于 2018-10-24T19:20:38.833 に答える