1

Hi am trying to use request module with promise in nodeJS.

Here is my Script

require('promise'); var request = require('request'); const requestget = function (url) { return new Promise((resolve, reject) { request(url, function (error, response, body) { if (!error && response.statusCode == 200) { resolve(body); } else { reject(error); } }); }); } requestget('http://www.modulus.io').then(console.log); 

but am getting the following error.

/home/xyz/reques.js:14 return new Promise((resolve, reject) { ^ SyntaxError: Unexpected token { at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3 

I really don't know what's wrong in this script any one can help me out.

2
  • 2
    choose between function (resolve, reject) { or (resolve, reject) => { Commented Mar 29, 2017 at 6:01
  • This worked for me. Thanks. I was using a very early version of Node. Arrow functions landed in version ~8. Commented Mar 27, 2018 at 16:00

2 Answers 2

5

On this line, you need to make sure you are defining a function. You can do this using arrow functions like so:

return new Promise((resolve, reject) => { 

Arrow functions have the syntax (param1, param2) => {function body}, which is the same thing as saying function(param1, param2) {function body}. You can read more about them here.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi tried the same but am getting error like this. '. /home/xyz/reques.js:5 return new Promise((resolve, reject) => { ^ SyntaxError: Unexpected token >'
try using return new Promise(function(resolve, reject) { instead then. you could also run node with node --harmony if you want the latest features like arrow functions as they are now the recommended way of doing this (to preserve scope)
thanks it works like charm return new Promise(function(resolve, reject) still I don't know why => function not working
you're using an older version of node. You can make it work if you run it with node --harmony.
2

You are trying to use arrow functions without arrows.

require('promise'); var request = require('request'); const requestget = function (url) { //Add the arrow on the below line return new Promise((resolve, reject) => { request(url, function (error, response, body) { if (!error && response.statusCode == 200) { resolve(body); } else { reject(error); } }); }); } requestget('http://www.modulus.io').then(console.log); 

2 Comments

still am getting the following error: /home/xyz/reques.js:5 return new Promise((resolve, reject) => { ^ SyntaxError: Unexpected token > at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3
You may have an older version of node, upgrade to anything past node v6 and you will have full support. If that's not an option just use new Promise(function(resolve, reject) { //Code here });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.