0

I am trying to use promises. Basically puting http connections in one js and calling from another js. But I am not able to do so. What's the mistake here?

http.js

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

module.exports.get = async (url) => {
  console.log("inside get method");
  const promise = new Promise(function (resolve, reject) {
    console.log("inside promise");
    fetch(url)
      .then(res => {
        console.log("inside fetch");
        resolve(res.json());
      })
      .catch(json => reject(Error(json)));
  })
  return promise;
}

 

main.js

'use strict';
const http = require('/opt/http.js')

module.exports.httpTest = async (event) => {
  let url = 'http://www.someurl.com/';
  console.log("calling get method");
  http.get(url).then(
  function (data) {
  console.log("inside http then")
  console.log(data);
}).catch(function (data) {
  console.log(data);
});
console.log("exited get method");
}

As you can see in http.js I have written a wrapper for GET request which I am trying to use in main.js.

When I execute main.js, nothing fails, but not get displayed on console. What I am doing wrong here?

UPDATE

I have added console logs everywhere... and when I call httpTest from anywhere, here is what I am getting

calling get method
inside get method
inside promise
exited get method

basically it's not going inside fetch

Joe
  • 41,484
  • 20
  • 104
  • 125
Abhijith Nagaraja
  • 3,370
  • 6
  • 27
  • 55
  • 1
    'when I execute main.js' - this, probably, I'm guessing you aren't invoking the code properly. There are some issues with your code in general, but nothing that would prevent you from seeing _something_ in the logs when ran. – James Jan 29 '21 at 02:52
  • fetch returns a promise.... It makes no sense to resolve and reject like you are doing. – epascarello Jan 29 '21 at 02:53
  • 1
    I don’t understand why you don’t just write `module.exports.get = async (url) => fetch(url).then((res) => res.json()).catch((err) => new Error(err));` or something similar. – Sebastian Simon Jan 29 '21 at 02:55
  • 2
    Did you forget to call `httpTest()`? (you also aren't using `event`) – mpen Jan 29 '21 at 03:01
  • 5
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Jan 29 '21 at 03:04
  • Besides the other two issues (you're uselessly wrapping fetch, and not calling httpTest anywhere), are you intending to import from `/opt`? – Zac Anger Jan 29 '21 at 03:11
  • I am calling httpTest from somewhere elsse... so I am calling it – Abhijith Nagaraja Jan 29 '21 at 03:16
  • main idea is to have a http with all the methods in there like GET, POST... and use wrappers that return promise. Based on status code, I will generate some standardised messages... so I need this pattern – Abhijith Nagaraja Jan 29 '21 at 03:18
  • If you use a full-featured library such as `got()` for your http requests, there is no need for the kind of wrapper you're doing. It has everything built-in. It's all promise-based and it will read/parse your JSON in one call without the extra chained promise that `fetch()` uses. – jfriend00 Jan 29 '21 at 04:00
  • I need to write some generic code inside these methods which I haven't put yet... This is the basic I am working on... And most of them only supports https and not http.. I am inside VPC and there is not https... so i need to use something very basic – Abhijith Nagaraja Jan 29 '21 at 04:02
  • Instead of reject(Error(json)) in catch block use reject(json). Looks fetch ends in error due to the additional wrapping in catch you are not getting the log printed. – Selvam M Jan 29 '21 at 04:37

1 Answers1

-1

Don’t create a useless extra promise.

  // this is a code smell
  const promise = new Promise(function (resolve, reject) {
    console.log("inside promise");
    fetch(url)
      .then(res => {
        console.log("inside fetch");
        resolve(res.json());
      })
      .catch(json => reject(Error(json)));
  })
  return promise;

Just return fetch(url); which already returns a promise. Your wrapper promise adds nothing.

Second, your exited get method is going to run outside the promise chain. If you want that to run after get finishes you need to await the http.get call inside HttpTest.

Joe
  • 41,484
  • 20
  • 104
  • 125