在自定义承诺上使用async await [英] Using async await on custom promise

查看:69
本文介绍了在自定义承诺上使用async await的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个返回一个promise的函数上使用async await但是out out im getting是 Promise {< pending> } 即可。在这里我使用名为convertFiletoPDF的函数返回一个promise。我需要获取输出(我在resolve()中提到的路径)。
当我用它时

Im trying to use async await on a function that returns a promise but the out put im getting is Promise { <pending> }. In here im using function called convertFiletoPDF which returns a promise. I need to get the output (the path that i have mention in resolve() ). When i use it as

convertFiletoPDF(file).then((result) => {
  console.log(result);
}).catch((err)=>{
  console.log(err);
});

它给出了预期的结果。下面的代码有什么问题?我对这些async await和promises很新。

it gives the expected result.Whats wrong with the code below? im quite new to these async await and promises.

 function convertFiletoPDF(file) {
  return new Promise(function(resolve, reject) {
    unoconv.convert(file, "pdf", function(
      err,
      result
    ) {
      if (err) {
        reject(err);
      }
      let File = file.substring(file.lastIndexOf("/")+1,file.lastIndexOf("."));
      // result is returned as a Buffer
      fs.writeFile(__dirname+"/files/converted/"+File+".pdf", result, error => {
        /* handle error */
        if (err) reject(error);
        else resolve("./files/converted/"+File+".pdf");
      });
    });
  });
}

async function myfunc(file){
  let res = await convertFiletoPDF(file);
  return res;
}

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


推荐答案

的返回值async function是一个promise,所以你的 console.log 输出就是这么。您需要通过 await (在另一个 async 函数内)消耗结果或使用然后 / catch (在另一个 async 函数内)。

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);

你需要这样做:

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
  }
})();

然后 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
  });

这篇关于在自定义承诺上使用async await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆