Initially, I managed to make this function work within a single file. However, I prefer organizing my code by splitting it into multiple files.
// library.js file
module.exports = {
get: () =>{
return new Promise((reject, resolve) =>{
return resolve(https.get('https://api.instagram.com/v1/users/self/?access_token=' + cred.access_token, (res) =>{
res.setEncoding('utf8');
return res.on('data', (data) =>{
return data;
});
}));
});
}
}
Initially, when I tried logging this, nothing was displayed.
// server.js file
igLib.get().then((data) => {
console.log("testing: " + data);
})
However, to my surprise, when I simply logged..
// server.js file
console.log(igLib.get());
I somehow managed to retrieve the data without using res.setEncoding('utf8')
.
Do you have any suggestions on what steps to take next?
Update: After struggling with making the promise work and appreciating those who provided valuable insights, I decided to utilize the request-promise module as an alternative solution. Here's the revised approach:
// library.js file
var instagramSelfUrl = 'https://api.instagram.com/v1/users/self/?access_token=' + cred.access_token;
module.exports = {
get: () =>{
return rp(instagramSelfUrl).then((res) =>{
return res;
});
}
}
Here is where I include the console.log statement:
// server.js file
igLib.get().then((data) =>{
console.log(data);
});
It's simpler, more efficient, and functional. If there are alternative solutions apart from relying on external modules, please feel free to share your thoughts and recommendations! Thank you for all the assistance and advice provided!