【问题标题】:How to handle [object Promise] in Discord.js google-images module如何在 Discord.js google-images 模块中处理 [object Promise]
【发布时间】:2018-01-15 10:02:50
【问题描述】:

我最近为我的 discord 机器人安装了一个名为 google-images 的节点模块,我设置了 CSE 和一个 API,但是当我将它设置为打印结果时,它返回一个 [Object Promise],我不知道如何让它输出一个链接/图像。

【问题讨论】:

标签: javascript node.js es6-promise discord.js


【解决方案1】:

您需要使用Promise.then()
当一个承诺被履行时,它“返回”的值作为参数传递给你在.then() 中的函数。

这是一个例子:

// This function returns a Promise
function takesTime() {
  return new Promise((res, rej) => {
    res("I'm \"returning\" this value");
  });
}

takesTime() // returns a Promise
  .then(value => { // the function you put into then is like a callback
    // inside it you can do your stuff with the value
    console.log(value);
  }).catch(console.error); // if the Promise is not fulfilled, you can catch errors

//Since takesTime() returned its value (even if it is a Promise) this will run first.
console.log("I'm the first.");

如果你需要一个好的指南来处理 Promise,你可以查看 MDN's guide on Using promises ;)

【讨论】:

    猜你喜欢
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 2017-07-26
    • 1970-01-01
    • 2021-10-23
    • 2021-07-14
    相关资源
    最近更新 更多