不能用异步执行http请求并等待 [英] can't do http request with async and await
问题描述
试图在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("http://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请求并等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!