不能用异步执行http请求并等待

编程入门 行业动态 更新时间:2024-10-25 04:19:59
本文介绍了不能用异步执行http请求并等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

试图在Node.js中使用 async 和 await 进行http请求,但出现错误.有任何想法吗?谢谢

Trying to use async and await to do http request in nodejs, but got error. Any ideas? Thx

got response: undefined /home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:539 callback(parsedData,res); ^ TypeError: callback is not a function at /home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:539:13 at Object.parse (/home/tom/learn/node/node_modules/node-rest-client/lib/nrc-parser-manager.js:151:3) at ConnectManager.handleResponse (/home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:538:32) at ConnectManager.handleEnd (/home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:531:18) at IncomingMessage.<anonymous> (/home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:678:34) at emitNone (events.js:110:20) at IncomingMessage.emit (events.js:207:7) at endReadableNT (_stream_readable.js:1059:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9)

这是脚本的源代码

var Client = require('node-rest-client').Client; var client = new Client(); async function test1() { response = await client.get("localhost/tmp.txt"); console.log("got response: "); console.log(response.headers); }; test1();

nodejs的版本为v8.4.0,在Ubuntu 14.04上.

nodejs is of version v8.4.0, on ubuntu 14.04.

推荐答案

async/await 不仅可以神奇地与需要回调的函数一起使用.如果client.get()希望将回调作为参数,则如果要使用它,则必须传递一个回调. async/await 与返回承诺的异步操作一起使用,并根据这些承诺进行操作.它们不可思议地让您跳过将回调传递到为回调设计的函数的过程.我建议您阅读更多有关如何实际使用 async 和 await 的文章.

async/await don't just magically work with functions that expect callbacks. If client.get() is expecting a callback as an argument, you HAVE to pass a callback if you're going to use it. async/await work with asynchronous operations that return promises and they operate on those promises. They do not magically let you skip passing callbacks to functions designed for a callback. I'd suggest a lot more reading about how to actually use async and await.

通常, async/await 的路径是首先设计所有 async 操作以使用promise和 .then()处理程序.然后,在所有工作完成之后,您可以将一个函数声明为要在其中使用await的异步函数,然后在这些异步声明的函数内部,您可以调用使用await返回promise的函数,而不是使用 .then()处理程序.这里没有魔术快捷键.从承诺设计开始.

In general, the path to async/await is to first design all your async operations to use promises and .then() handlers. Then, after that is all working, you can declare a function as async that you want to use await in and then inside those async-declared functions you can call functions that return promises with await instead of using .then() handlers with them. There are no magic shortcuts here. Start with a promise design.

这是一个简单的promise示例:

Here's a simple promise example:

// asynchronous function that returns a promise that resolves to // the eventual async value function delay(t, val) { return new Promise(resolve => { setTimeout(() => { resolve(val); }, t); }); } function run() { return delay(100, "hello").then(data => { console.log(data); return delay(200, "goodbye").then(data => { console.log(data); }); }).then(() => { console.log("all done"); }); } run();

而且,这与使用 async/await 相同:

// function returning a promise declared to be async function delay(t, val) { return new Promise(resolve => { setTimeout(() => { resolve(val); }, t); }); } async function run() { console.log(await delay(100, "hello")); console.log(await delay(200, "goodbye")); console.log("all done"); } run();

这两个示例均产生相同的输出和相同的输出时序,因此希望您可以看到从promise到async/await的映射.

Both of these examples produce the same output and same output timing so hopefully you can see the mapping from promises to async/await.

更多推荐

不能用异步执行http请求并等待

本文发布于:2023-11-13 01:02:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不能用   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!