【问题标题】:Another Node.js newb who doesn't get it另一个不明白的 Node.js 新手
【发布时间】:2018-12-03 15:26:12
【问题描述】:

已编辑:调整了我的叙述,并尝试将输出添加到代码中,如示例所示,但它不起作用。我做错了什么?

嗨,比我优秀的专家或爱好者,

问题是“如何正确地从 node.js 中的异步函数获取输出作为返回”。这些示例都谈到了这个神秘的回调函数,但在我的代码上下文中,我看不到它是如何应用或实现的。

是的,这个问题已经被问过很多次了,如果我不得不再问一次,那是因为提供的解释没有让这个新手理解。是的,我花了将近 24 小时左右的时间来尝试遵循示例、文档和其他帖子,但我没有找到一个解释得足够清楚以至于我可以应用到我的代码的帖子。

异步的概念是有意义的,代码可以运行,但在这种情况下,https 调用没有。代码不会等待 https 调用。完成后,您必须以某种方式获取结果。虽然我还没有找到它的实用性,但我相信我会继续学习为什么 node.js 如此特别。假设我的理解大部分是正确的,我的问题仍然是一样的。概念是一回事,应用和语法是另一回事。

这似乎是一个常见问题,几乎每个新手都遇到了麻烦。

到目前为止,似乎没有任何示例或解释能够阐明我在何处或如何使用。我知道还有其他模块可以以不同方式处理这些问题,但我相信除非我正确理解,否则我不会理解它适用的“为什么/如何”。

由于我是 node.js 的新手,我很想学习,请随意扩展我的代码的任何方面。

如果有人发现此问题,此代码将从官方 Clash Royal API 获取数据,您需要为其注册 IP 并从 https://developer.clashroyale.com 获取令牌。

app.js

require('dotenv').config();
var func = require('./functions.js');

console.log(func.uChests(process.env.MyPlayer));  //this should output the value

functions.js

require('dotenv').config();
//console.log('Loaded Functions')
module.exports.uChests = func_uChests

//Clearly wrong application
//function func_uChests (playerID) {
function func_uChests (playerID,output) {
    //console.log('uChests')
    var http = require("https");

    var options = {
      "method": "GET",
      "hostname": "api.clashroyale.com",
      "port": null,
      "path": "/v1/players/%23"+ playerID + "/upcomingchests",
      "headers": {
        "content-length": "0",
        "authorization": "Bearer " + process.env.Token,
        "accept": "application/json"
      }
    };

    var req = http.request(options, function (res) {
      var chunks = [];

      res.on("data", function (chunk) {
        chunks.push(chunk);
      });

      res.on("end", function () {
        var body = Buffer.concat(chunks);
        console.log(body.toString());

        /*  example output
        {"items":[{"index":0,"name":"Magical Chest"},{"index":1,"name":"Silver Chest"},{"index":2,"name":"Silver Chest"},{"index":3,"name":"Golden Chest"},{"index":4,"name":"Silver Chest"},{"index":5,"name":"Silver Chest"},{"index":6,"name":"Silver Chest"},{"index":7,"name":"Golden Chest"},{"index":8,"name":"Silver Chest"},{"index":22,"name":"Legendary Chest"},{"index":40,"name":"Giant Chest"},{"index":76,"name":"Super Magical Chest"},{"index":77,"name":"Epic Chest"}]}
{"items":[{"index":0,"name":"Magical Chest"},{"index":1,"name":"Silver Chest"},{"index":2,"name":"Silver Chest"},{"index":3,"name":"Golden Chest"},{"index":4,"name":"Silver Chest"},{"index":5,"name":"Silver Chest"},{"index":6,"name":"Silver Chest"},{"index":7,"name":"Golden Chest"},{"index":8,"name":"Silver Chest"},{"index":22,"name":"Legendary Chest"},{"index":40,"name":"Giant Chest"},{"index":76,"name":"Super Magical Chest"},{"index":77,"name":"Epic Chest"}]}
*/
      });
    });

    req.end();
  }

//Clearly wrong application
function uChests(input, output) {
    func_uChests(input, output);   
    console.log(output);
};

【问题讨论】:

  • 更正这是许多其他类似问题的重复,但我不明白。
  • “我不明白”是不可操作的:你不明白什么?有大量的教程解释了 JS 中的异步编程:不知道认知脱节是什么是不可能的帮助超出教程中已经显示的内容 - 请注意 SO 不是教程网站。
  • 我很抱歉我没有/无法清楚地表达我对如何应用所需更改的理解。如果我比我理解得更好,那么我可能不会寻求帮助。我将尝试调整我的问题以澄清。感谢您指出它不够清楚。
  • 它不会“等待”,因为没有什么可以告诉它的。您可以传递在“结束”处理程序中调用的函数(回调函数),也可以在结束处理程序中解决承诺。 (至少这些是典型的选项;您也可以发出事件等。)但是这些在所有异步教程中都有讨论。您尝试过使用哪些教程?

标签: node.js asynchronous callback


【解决方案1】:

我认为你应该更好地理解 node 的异步性质,你可以将值返回给调用者语句的唯一方法是使用函数参数或 Async/Await 和 Promises API,看看下面。

' // 从函数参数返回

myAsyncFunction(函数(值){

console.log(值) })

// 或使用 Promise API 让值 = 等待 myAsyncFunction()´

【讨论】:

  • 什么是“价值”,我用 body.tostring() 试过了,它不起作用。这部分是我迷失的地方。
  • @Maramor "value" 是传递给函数的任何内容。
  • :) 好的,我知道函数是如何工作的,但是在我的代码中我输入了 playerID,但我不希望 playerID 作为返回值,而且我看不到您的示例如何解释这个问题。正如您在上面的 cmets 中指出的那样,我的问题不够清楚,因此我将尝试澄清。感谢两位抽出宝贵时间。
猜你喜欢
  • 2012-09-09
  • 1970-01-01
  • 2011-04-13
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多