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
question from:
https://stackoverflow.com/questions/65948076/issue-with-promises 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…