javascript - 返回自定义 promise

标签 javascript node.js promise invocation

我在获取与我正在开发的小应用程序一起使用的自定义 promise 时遇到一些问题。

这是我的 app.js 的缩写版本

var Promise = require('promise');
var Commands = require('./commands');

var respond = function() {
    var messageToRespondWith = command[0].response;
    if(typeof(command[0].response) == "function"){
        console.log('is function');
        command[0].response().then(console.log('finished'));
    }
    var response = template(messageToRespondWith, {user: user, channel: channel, message: message});
    client.say(channel, response);
};

命令看起来像这样

var config = require('./config');
var Promise = require('promise');

module.exports = [
  {
    trigger: "!random",
    response: function(){
      var unirest = require('unirest');
      var promise = new Promise(function(resolve, reject) {
      unirest.post("https://andruxnet-random-famous-quotes.p.mashape.com/cat=movies")
      .header("X-Mashape-Key", config.mashapeKey)
      .header("Content-Type", "application/x-www-form-urlencoded")
      .header("Accept", "application/json")
      .end(function (result) {
        console.log(result.status);
        // Handle errors
        if(result.status != 200){
          return promise.reject(result.body);
        }else{
          var json = JSON.parse(result.body);
          return promise.resolve(json.quote);
        }
      });
    });
  },
  permission: null
  },
]

app.js 代码检查返回的值类型是否为函数,如果是,则执行该函数,并应将其保存到变量并将其传递给下一个变量功能。

我需要使用 Promise,因为显然底部函数调用 client.say() 不会等待来自 commands.js< 的函数 API 调用的返回值 文件。

当我尝试执行此代码时,在我的终端中出现此错误...

TypeError: Cannot read property 'then' of undefined
at respond (/***/app.js:39:28)

所以我不太确定为什么会发生这种情况,我仍在学习 Node 和做事的最佳方法,所以也许我完全走错了路。

基本功能

这是一个 twitch IRC 聊天机器人,它将读取输入的命令并在聊天中使用预定消息进行响应。在某些情况下,我想在吐出消息之前执行某种功能。这是其中之一。您可以查看我的完整 app.js 代码 here我不想粘贴整个内容,因为它很长并且包含我认为与此处不相关的部分。

最佳答案

您需要在此函数末尾返回 promise 。默认情况下,您的函数返回“未定义”,因此出现错误。您需要同步返回 Promise,然后异步解析它。

在您的 end 回调中,您返回了 promise.resolve(..) 但这是不正确的。您需要调用 promise.resolve(..) 但不需要返回它。

   response: function(){
      var unirest = require('unirest');
      var promise = new Promise(function(resolve, reject) {
      unirest.post("https://andruxnet-random-famous-quotes.p.mashape.com/cat=movies")
      .header("X-Mashape-Key", config.mashapeKey)
      .header("Content-Type", "application/x-www-form-urlencoded")
      .header("Accept", "application/json")
      .end(function (result) {
        console.log(result.status);
        // Handle errors
        if(result.status != 200){
          promise.reject(result.body);
        }else{
          var json = JSON.parse(result.body);
          promise.resolve(json.quote);
        }
      });
      return promise;
    });

关于javascript - 返回自定义 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31483666/

相关文章:

Javascript Promise 与 Mysql 链接

javascript - Jquery 不触发动态创建的表

javascript - 如何在 TextInput 中按 'Enter' 调用函数 - React Native

node.js - 错误 :"Property ' 类型 'LoopBackApplication' 上不存在注册表”

javascript - 如何在 vanilla javascript 中将 promise 链接到 for 循环中

typescript - 带有 readline(询问者)问题的异步/等待循环

c# - 当 web 服务方法抛出异常时如何获取错误消息。

javascript - 有没有办法确认数据是否通过网络浏览器传输到谷歌浏览器中的服务器

javascript - 如何保存应用程序的登录次数?(nodejs,JS,mongodb)

javascript - 在对象属性中要求和调用某些东西