ERC-721 Contract – How to Resolve tokenURI Returning a Promise Instead of a String

erc-721ethers.jshardhatmemoryopenzeppelin-contracts

In my solidity contract, the function for tokenURI looks like this:

function tokenURI(uint256 tokenId)
  public
  view
  virtual
  override
  returns (string memory)
{
  require(
    _exists(tokenId),
    "ERC721Metadata: URI query for nonexistent token"
  );
  
  if (revealed == false) {
    return string(notRevealedUri);
  }

  string memory currentBaseURI = _baseURI();
  return
    bytes(currentBaseURI).length > 0
    ? string(
      abi.encodePacked(
        currentBaseURI,
        tokenId.toString(),
        baseExtension
      )
    )
  : "";
}

Notice that I have a bool to determine whether or not the "revealed" tokenURI has been set. The problem I've having is when revealed is set to false but I have called the setNotRevealedURI function to actually set the notRevealedUri to a value. I want the function tokenURI to give me this notRevealedUri string but instead it gives me a Promise:

> token.tokenURI(1);
Promise {
  <pending>,
  [Symbol(async_id_symbol)]: 501,
  [Symbol(trigger_async_id_symbol)]: 7,
  [Symbol(destroyed)]: { destroyed: false }
}
> 

I've been interacting with the contract using hardhat btw.

So how do I get this function to output a string?

Best Answer

All contract interactions are asynchronous, you will need to use await or then.

await token.tokenURI(1);