Avatar of Andres Rodriguez
Andres RodriguezFlag for United States of America asked on

Promisifying my https.get() function is returning Promise { <pending> }

I'm using https.get() to fetch an api, however to use async await I'm trying to promisify the function. While I can retrieve the data, if I try to do anything to access the data or loop through the object, or access anything in the object, it will read ``'Cannot read property '0' of undefined'.  However, if I try to resolve the data with 'newdata[0]' as shown below, it will return with just `Promise { <pending> }. 

Why am I not able to get asynchronous operation I'm trying for below? Or is there a better way to do this? Basically I'm just trying to make an asynchronous operation with async await, and I need to use https (not fetch, axios, etc). 


const https = require('https');
const myfunc = ((url) => {
  return new Promise((resolve, reject) => {
    https.get(url, res => {
      let data = [];
      res.on('data', chunk => {
      res.on('end', () => {
        let newdata = JSON.parse(Buffer.concat(data).toString())
      }).on('error', err => {
        console.log('Error: ', err.message);

async function getApi() {

  let url = [redacted url];
  await myfunc(url)
    .then(data => {
    .catch(err => {

Open in new window

JavaScriptProgrammingWeb Development

Avatar of undefined
Last Comment
Andres Rodriguez

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Andres Rodriguez

It is a JSON object it should look like this

  page: 1,
  per_page: 500,
  total: 1,
  total_pages: 1,
  data: [
      date: '5-January-2000',
      open: 5265.09,
      high: 5464.35,
      low: 5184.48,
      close: 5357

Open in new window

Here is the full code slightly refactored from the original question. Still getting Promise { <pending> }

const https = require('https');

const myRequest = ((url) => {
  return new Promise((resolve, reject) => {
    https.get(url, res => {
      let data = [];
      res.on('data', chunk => {
      res.on('end', () => {
      }).on('error', err => {
        console.log('Error: ', err.message);

async function getStockInformation(date) {
  let url = `https://jsonmock.hackerrank.com/api/stocks?date=${date}`;

  await myRequest(url)
    .then(data => {
     const {open, high} = data.data[0];
      return `Open: ${open} \n High: ${high}`
    .catch(err => {

let date = '5-January-2000';


Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck