如何在NodeJs中使用axios发出请求而无需等待响应?[关闭]

vzgqcmou  于 2023-05-28  发布在  Node.js
关注(0)|答案(2)|浏览(205)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
11小时前关闭
Improve this question
我正在设置多个微服务,其中我希望向服务器发出一些REST调用,而不等待它响应。
我已经注意到axios是异步的,并等待响应。是否可以简单地提出请求并继续?有没有其他的套餐?

k4emjkb1

k4emjkb11#

你不能只是等待回应。在大多数情况下,如果你想要得到响应,你会做这样的事情:

axios.get('/user?ID=12345')
  .then(function (response) {
    // handle success
    doSomethingWithResponseFn(response)
  })

如果你真的不在乎别人的React,那就打电话

axios.get('/your-url')
// ...rest of your code

正如你所能想象的,没有保证它完成或工作,但这听起来像是你所追求的。

s8vozzvw

s8vozzvw2#

您可以考虑的一个替代方案是使用node-fetch包。
使用node-fetch发出异步请求而不等待响应时,可以使用fetch

const fetch = require('node-fetch');

fetch('https://example.com/api/endpoint', {
 method: 'POST',
 body: JSON.stringify({ data: 'example' }),
 headers: { 'Content-Type': 'application/json' }
})
.catch(error => {
  console.error('An error occurred:', error);
 });

  console.log('Request sent. Continuing execution...');

相关问题