jquery 如何在一个函数中使用另一个函数中的变量- javascript

vfh0ocws  于 2022-11-03  发布在  jQuery
关注(0)|答案(2)|浏览(249)

这里有两个相互依赖的函数

async function block() {
  let getBlock = await web3.eth.getBlock('latest');
  console.log(getBlock['number']);
  let lblock = getBlock['number'] - 8;
  console.log(lblock);
}
block();

pcsfactoryC.events.PairCreated({
  fromBlock: lblock
})

使用async函数,我得到了最新的块,然后进行了一些计算,得到了变量lblock,我在下面的函数中使用它作为{fromBlock: lblock}的输入
那么,如何在另一个函数中使用变量“lblock”呢?

zmeyuzjn

zmeyuzjn1#

Return it from the function.
Since block() is async , it returns a promise. You either have to use await to wait for it (you can only do this if the other function is also async , or use .then() to execute a function when the promise is resolved.

async function block() {
  let getBlock = await web3.eth.getBlock('latest');
  console.log(getBlock['number']);
  let lblock = getBlock['number'] - 8;
  console.log(lblock);
  return lblock;
}
block().then(lblock => pcsfactoryC.events.PairCreated({
  fromBlock: lblock
}));
72qzrwbm

72qzrwbm2#

设函数abc(){ a=10 }
函数xyz(){ alert(a)}
abc()xyz()

相关问题