Using async await on custom promise

后端 未结 3 2019
情深已故
情深已故 2020-12-21 07:59

Im trying to use async await on a function that returns a promise but the out put im getting is Promise { }. In here im using f

相关标签:
3条回答
  • 2020-12-21 08:18

    The return value of an async function is a promise, so naturally that's what your console.log outputs. You need to either consume the result via await (within another async function) or use then/catch (within another async function).

    This is what you're currently doing:

    function convertFiletoPDF(file) {
      return new Promise(function(resolve, reject) {
        setTimeout(resolve, 400, "Done");
      });
    }
    
    async function myfunc(file){
      let res = await convertFiletoPDF(file);
      return res;
    }
    
    let res = myfunc("some file");
    console.log(res);

    You need to be doing either this:

    function convertFiletoPDF(file) {
      return new Promise(function(resolve, reject) {
        setTimeout(resolve, 400, "Done");
      });
    }
    
    async function myfunc(file){
      let res = await convertFiletoPDF(file);
      return res;
    }
    
    (async() => {
      try {
        let res = await myfunc("some file");
        console.log(res);
      } catch (e) {
        // Deal with the fact there was an error
      }
    })();

    or with then and catch:

    function convertFiletoPDF(file) {
      return new Promise(function(resolve, reject) {
        setTimeout(resolve, 400, "Done");
      });
    }
    
    async function myfunc(file){
      let res = await convertFiletoPDF(file);
      return res;
    }
    
    myfunc("some file")
      .then(res => {
        console.log(res);
      })
      .catch(e => {
        // Deal with the fact there was an error
      });

    0 讨论(0)
  • 2020-12-21 08:30

    Someone might find this example from my code useful. You can wrap it in a promise and then resolve the custom promise and then call another promise to confirm the receipt of the original web3 call.

    return new Promise((resolve, reject) => {
        tokenContract.methods.approve(
            exchangeAddress, 
            BIG_NUMBER_1e50
        )
        .send({ from })
        .once('transactionHash')
        .once('receipt', receipt => resolve(receipt))
        .on('confirmation')
        .on('error', err => reject(err))
        .then( receipt => // will be fired once the receipt its mined
            console.log(receipt),
        );
    });
    
    0 讨论(0)
  • 2020-12-21 08:36
    convertFiletoPDF() 
    

    This function run and returned a Promise. This is fine.

    myfunc()
    

    Lets say myfunc takes 10 seconds. Javascript starts to wait newly created thread result from libuv via event loop mechanism. So, Javascript says, "That one is async, I will not wait, when it finishes it will let me know and i will run my then callback and then I will proceed with its output."

    Javascript keeps his promise. Tries to run next below lines. myFunch is still working. Output is not ready yet. Returns undefined.

    let res = myfunc(file);
    console.log(res);
    

    You get undefined.

    0 讨论(0)
提交回复
热议问题