我怎样才能一遍又一遍地执行异步功能?

How can I do a async function over and over?

如何反复执行异步函数?我曾尝试在 while 循环中执行此操作,但它只执行第一行 console.log 而没有其他内容。

import fs from 'fs-extra'
import fetch from 'node-fetch'

function wait(milliseconds) {
    const date = Date.now();
    let currentDate = null;
    do {
      currentDate = Date.now();
    } while (currentDate - date < milliseconds);
}

async function gasFee() {
    console.log("fetching ETH Price")
    var ethprice = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd')
    var ethPriceJSON = await ethprice.json()
    console.log("fetching Ethermine GWEI")
    var etherminegwei = await fetch('https://api.ethermine.org/poolStats')
    var ethermineGweiJSON = await etherminegwei.json()
    var ethPrice = ethPriceJSON.ethereum.usd
    var ethermineGwei = ethermineGweiJSON.data.estimates.gasPrice
    var gweiPrice = ethPrice/1000000000
    var price = ethermineGwei * gweiPrice * 21000 .toFixed(2)
    var timeNow = new Date()
    if (price > 5) {
        console.log("Gas Price Logged")
        fs.appendFileSync('gasPrice.txt', '$' + price + ' | ' + timeNow + '\r\n')
    }
    else {return}
    if (price <= 5) {
        console.log(`Gas Price is $${price} at ${timeNow}`)
        fs.appendFileSync('lowGasPrice.txt', '$' + price + ' | ' + timeNow + '\r\n')
    }
    else {return}
}

while (true) {
    gasFee()
    wait(1500)
}

您的等待函数不是基于 promise 的异步函数,您需要更改它。 此外,您需要等待 getFee() 函数进行异步执行。

import fs from "fs-extra";
import fetch from "node-fetch";

const wait = ms => new Promise((resolve, reject) => setTimeout(resolve, ms));

async function gasFee() {
  console.log("fetching ETH Price");
  var ethprice = await fetch(
    "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"
  );
  var ethPriceJSON = await ethprice.json();
  console.log("fetching Ethermine GWEI");
  var etherminegwei = await fetch("https://api.ethermine.org/poolStats");
  var ethermineGweiJSON = await etherminegwei.json();
  var ethPrice = ethPriceJSON.ethereum.usd;
  var ethermineGwei = ethermineGweiJSON.data.estimates.gasPrice;
  var gweiPrice = ethPrice / 1000000000;
  var price = ethermineGwei * gweiPrice * (21000).toFixed(2);
  var timeNow = new Date();
  if (price > 5) {
    console.log("Gas Price Logged");
    fs.appendFileSync("gasPrice.txt", "$" + price + " | " + timeNow + "\r\n");
  } else {
    return;
  }
  if (price <= 5) {
    console.log(`Gas Price is $${price} at ${timeNow}`);
    fs.appendFileSync(
      "lowGasPrice.txt",
      "$" + price + " | " + timeNow + "\r\n"
    );
  } else {
    return;
  }
}

(async function run() {
  while (true) {
    await gasFee();
    await wait(1500);
  }
})();

为了赞美已接受的答案,您可以考虑使用内置的 javascript 函数 setInterval()

这需要一个函数作为回调,每 x 毫秒执行一次。函数returns一个ID,以后可以用来取消间隔:

var gasFee = function () {
    console.log("fetching ETH Price");
    // ... Rest of function
  }

  // Call gasFee() every 1500 MS
  var gasFeeIntervalID = setInterval(gasFee, 1500);

  // Cancel execution if needed
  clearInterval(gasFeeIntervalID);