node.js - 了解异步函数 --> 第二个函数运行很快
问题描述
我进行 API 调用以获取第二个函数所需的令牌。但是这个运行很快,所以没有数据。(我得到一个错误,json 意外结束)。在此之后,我打了 4 次矿石电话。我在谷歌应用程序脚本中制作了这个,但漏洞脚本大约需要 17 分钟。所以我想在 NodeJS 中创建它并部署在 firebase 上,然后为应用程序脚本创建一个端点以获取已经准备好的数据。
第一个函数
const express = require('express');
const app = express();
const fetch = require('node-fetch');
const resid = require('./calls.js');
/*
Here we start with the request to get a new token. And pass this on in the request's later on.
*/
async function getToken() {
const config = {
method: 'post',
headers: {}
};
const url = 'https://theapirurl.com';
const response = await fetch(url, config);
const json = await response.json();
const token = json['access_token'];
await resid.getReservationIds(token);
}
getToken()
第二个功能(在calls.js上)
const fetch = require('node-fetch');
async function getReservationIds(token) {
const url = "https://theapiurl.com";
const config = {
method: "get",
muteHttpExceptions: true,
headers: {
"Authorization": "bearer" + token
}
};
const respone = await fetch(url, config);
const json = await respone.json();
//console.log(json);
}
module.exports = {
resid: getReservationIds()
}
我究竟做错了什么?
编辑
现在我得到这个错误:
(node:43371) UnhandledPromiseRejectionWarning: FetchError: invalid json response body at https://apiurlfromcallsjs.com reason: Unexpected end of JSON input
at /Users/remco/functions/node_modules/node-fetch/lib/index.js:272:32
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async getReservationIds (/Users/remco/functions/calls.js:20:18)
(node:43371) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:43371) [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.
(node:43371) UnhandledPromiseRejectionWarning: TypeError: getReservationIds is not a function
at getToken (/Users/remco/functions/index.js:23:11)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:43371) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
解决方案
您正在立即调用第二个函数,而不是等待第一个函数完成:
module.exports = {
resid: getReservationIds()
}
所以当你跑
const resid = require('./calls.js');
此时,第二个函数已经在运行(并且resid.getReservationIds
是一个 Promise,而不是一个函数)。
只导出函数:
module.exports = getReservationIds;
然后导入并调用它:
const getReservationIds = require('./calls.js');
// ...
await getReservationIds(token);
推荐阅读
- ios - NSEntityDescription.entity crashes app (SIGABRT)
- javascript - 使用 jQuery 将 HTML 属性设置为 JSON 对象
- python - Python 按列分组并在另一列中计数字符串
- c# - C# 中的加扰字符串操作
- python - 我如何允许用户输入添加到字典
- javascript - 轮盘赌,选择图像
- python - Python:如何最好地处理开发同时使用它的库和应用程序?
- xml - infopath xml 上的尾随空值
- string - 用字符串更改的不同值替换字符串中的值
- javascript - 我正在使用 javascript 在模式中创建一个表单,但它不适用于 ajax