我有以下代码,我正在尝试用node.js运行:
ethereum-block-by-date.js
module.exports = class {
constructor(web3) {
this.web3 = web3.constructor.name === 'Web3' ? web3 : { eth: web3 };
this.checkedBlocks = {};
this.savedBlocks = {};
this.requests = 0;
}
.
.
.
async getEvery(duration, start, end, every = 1, after = true) {
start = moment(start), end = moment(end);
let current = start, dates = [];
while (current.isSameOrBefore(end)) {
dates.push(current.format());
current.add(every, duration);
}
if (typeof this.firstBlock == 'undefined' || typeof this.latestBlock == 'undefined' || typeof this.blockTime == 'undefined') await this.getBoundaries();
return await Promise.all(dates.map((date) => this.getDate(date, after)));
}
.
.
.和getBlockNumber.js
const EthDater = require('ethereum-block-by-date');
const { ethers } = require('ethers');
const url = "node url";
const provider = new ethers.providers.WebSocketProvider(url);
const dater = new EthDater(
provider
);
let blocks = await dater.getEvery('hours', '2018-11-15T00:00:00Z', '2018-11-22T00:00:00Z');
console.log(blocks);当我运行node getBlockNumber.js时,我得到了引用let blocks = await dater.getEvery()函数调用的SyntaxError: await is only valid in async function。
如果getEvery()在ethereum-block-by-date.js中被定义为aync,那么它为什么要返回这个SyntaxError?当我从dater.getEvery()中删除await时,它会返回一个Promise对象。
发布于 2021-07-18 23:30:02
当我将await dater.getEvery()放在async函数中,然后调用该函数时,这个问题就解决了:
async function getBlocks() {
let blocks = await dater.getEvery('hours', '2018-11-15T00:00:00Z', '2018-11-22T00:00:00Z');
console.log(blocks);
}
getBlocks();https://stackoverflow.com/questions/68430408
复制相似问题