【问题标题】:async/await doesn't work with promisifyasync/await 不适用于 promisify
【发布时间】:2018-03-24 19:31:19
【问题描述】:

我正在尝试使用util.promisify 函数,但我的代码不起作用。我想我不明白 promises/promisify 是如何工作的。如果我使用new Promise,我的代码可以工作,但如果我使用promisify,它就不行。

如果我运行这段代码:

const oembed = require('oembed-auto');
const { promisify } = require('util');

const test = () => {
  return new Promise((resolve, reject) => {
    oembed("http://www.youtube.com/watch?v=9bZkp7q19f0", function (err, data) {
      resolve(data);
    });
  });
};

module.exports.getAllInterests = async () => {
  const data = await test();
  console.log(data);
  return data;
};

我得到了预期的结果:

{ html: '<iframe width="480" height="270" src="https://www.youtube.com/embed/9bZkp7q19f0?feature=oembed" frameborder="0" allowfullscreen></iframe>',
  author_url: 'https://www.youtube.com/user/officialpsy',
  thumbnail_url: 'https://i.ytimg.com/vi/9bZkp7q19f0/hqdefault.jpg',
  width: 480,
  height: 270,
  author_name: 'officialpsy',
  thumbnail_height: 360,
  title: 'PSY - GANGNAM STYLE(강남스타일) M/V',
  version: '1.0',
  provider_url: 'https://www.youtube.com/',
  thumbnail_width: 480,
  type: 'video',
  provider_name: 'YouTube' }

如果我运行这段代码:

const oembed = require('oembed-auto');
const { promisify } = require('util');

const test = () => {
  promisify(oembed)("http://www.youtube.com/watch?v=9bZkp7q19f0").then((data) => {
    resolve(data);
  });
};

module.exports.getAllInterests = async () => {
  const data = await test();
  console.log(data);
  return data;
};

异步/等待不起作用,我得到:

undefined
(node:66423) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: resolve is not defined
(node:66423) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

  • 我的意思是...resolve 在第二种情况下是未定义的。否则它为什么会给你一个错误说明
  • @KevinB 如果我把它改成return,结果是undefined,没有错误信息resolve is not defined
  • 这两个问题不相关。
  • 我的意思是,这不是very well documented 或任何东西。 /s 文档中的示例甚至直接按照你需要的代码逻辑。

标签: javascript node.js asynchronous es6-promise


【解决方案1】:

没有值是returned 来自test,resolve 是undefined 在.then() 内

const test = () => promisify(oembed)("http://www.youtube.com/watch?v=9bZkp7q19f0");

module.exports.getAllInterests = async () => {
  let data;
  try {
    data = await test();
    console.log(data);
  } catch (err) {
    console.error(err);
    throw err;
  }
  return data;
};

getAllInterests().then(res => console.log(res), err => console.err(err));

【讨论】:

  • 如果我把它改成return,结果是undefined,没有错误信息resolve is not defined
  • 你根本不需要.then(data =&gt; data)。
【解决方案2】:

你的测试函数必须是异步的,你只需要在 promisify(oembed) 上使用 Await。请注意,您需要在异步函数中使用 getAllInterests,在这种情况下,我将它放在异步闭包中。 (我自己测试了代码,它运行良好)。而且它也更易于调试和维护。

const oembed = require('oembed-auto');
const { promisify } = require('util');

const test = async () => {
  return await promisify(oembed)("http://www.youtube.com/watch?v=9bZkp7q19f0");
}

const getAllInterests = async () => {
  const data = await test();
  console.log(data);
  return data;
};

(async ()=> {
    await getAllInterests();
})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2018-07-10
    • 2021-06-18
    • 2019-05-30
    • 2021-05-10
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多