如何在Discord.js google-images模块中处理[object Promise]

时间:2017-08-08 01:44:57

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

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

1 个答案:

答案 0 :(得分:0)

您需要使用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.");

如果您需要良好的指南来实现Promises,可以查看MDN's guide on Using promises;)