node.js - can't do http request with async and await -


trying use async , await http request in nodejs, got error. 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 not function     @ /home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:539:13     @ object.parse (/home/tom/learn/node/node_modules/node-rest-client/lib/nrc-parser-manager.js:151:3)     @ connectmanager.handleresponse (/home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:538:32)     @ connectmanager.handleend (/home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:531:18)     @ incomingmessage.<anonymous> (/home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:678:34)     @ emitnone (events.js:110:20)     @ incomingmessage.emit (events.js:207:7)     @ endreadablent (_stream_readable.js:1059:12)     @ _combinedtickcallback (internal/process/next_tick.js:138:11)     @ process._tickcallback (internal/process/next_tick.js:180:9) 

here source code script

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 of version v8.4.0, on ubuntu 14.04.

async/await don't magically work functions expect callbacks. if client.get() expecting callback argument, have pass callback if you're going use it. async/await work asynchronous operations return promises , operate on promises. not magically let skip passing callbacks functions designed callback. i'd suggest lot more reading how use async , await.

in general, path async/await first design async operations use promises , .then() handlers. then, after working, can declare function async want use await in , inside async-declared functions can call functions return promises await instead of using .then() handlers them. there no magic shortcuts here. start promise design.

here's simple promise example:

// asynchronous function returns promise resolves // 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(); 

and, here's same adapted use async/await:

// function returning promise declared 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(); 

both of these examples produce same output , same output timing can see mapping promises async/await.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -