Asynchronous functions return Promises, not values
P粉464208937
P粉464208937 2023-10-19 18:54:01
0
2
402

I'm trying to understand how async/await works with Promise.

async function latestTime() { const bl = await web3.eth.getBlock('latest'); console.log(bl.timestamp); // Returns a primitive console.log(typeof bl.timestamp.then == 'function'); //Returns false - not a promise return bl.timestamp; } const time = latestTime(); // Promise {  }

From what I understand, await is supposed to be blocking, and in the code above, it seems to prevent using the primitivetimestampto return an objectbl. My function then returns the original value, but the time variable is set to the pending promise instead of that original value. What did I miss?

P粉464208937
P粉464208937

reply all (2)
P粉099985373

Asynchronous prefix is a wrapper for Promises.

async function latestTime() { const bl = await web3.eth.getBlock('latest'); console.log(bl.timestamp); // Returns a primitive console.log(typeof bl.timestamp.then == 'function'); //Returns false - not a promise return bl.timestamp; }

the same with

function latestTime() { return new Promise(function(resolve,success){ const bl = web3.eth.getBlock('latest'); bl.then(function(result){ console.log(result.timestamp); // Returns a primitive console.log(typeof result.timestamp.then == 'function'); //Returns false - not a promise resolve(result.timestamp) }) }
    P粉002023326

    asyncFunctions always return a Promise. This is how it reports the completion of asynchronous work. If you're using it inside anotherasyncfunction, you can useawaitto wait for its promise to resolve, but in a non-asyncfunction (usually at the top level or in an event handler), you have to use Promise directly, for example:

    latestTime() .then(time => { console.log(time); }) .catch(error => { // Handle/report error });

    ...However, if you do this at the top level of a JavaScript module, all modern environments now support top-levelawaitinmodules:

    const time = await latestTime();

    (Note that if this Promise is rejected, your module will fail to load. If your module works meaningfully even if the Promise fails, be sure to wrap it in atry/catchHandles promise rejection.)


    Itmay(or may not) reveal something in the form of explicit promise callback terms that gives us an idea of how the JavaScript engine handles yourasyncfunction under the hood:

    function latestTime() { return new Promise((resolve, reject) => { web3.eth.getBlock('latest') .then(bl => { console.log(bl.timestamp); console.log(typeof bl.timestamp.then == 'function'); resolve(bl.timestamp); }) .catch(reject); }); }

    Some important notes:

    • The function you pass tonew Promise(thepromise executorfunction) is called synchronously bynew Promise.
      • This is why the operation starts, callingweb3.eth.getBlocksynchronously to start the work.
    • Any errors (etc.) thrown in the Promise executor will be caught by thenew Promiseand converted into a Promise rejection.
    • Any errors thrown in the Promise callback (such as thethenerrors we pass) will be caught and converted into rejections.
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!