Issue with promises

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) => {
  const promise = new Promise(function (resolve, reject) {
    fetch(url)
      .then(res => resolve(res.json()))
      .then(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/';
  http.get(url).then(
    function (data) {
      console.log(data);
    },
    function (error) {
      console.log(error);
    }
  )
}

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?

159 thoughts on “Issue with promises”

Leave a Comment