I am trying to store result in another array after working on a array data but I always get [undefined,undefined]
as my response array.
This is my code snippet, I am not sure if this is correct way to get the data into an array.
function ping(urls) {
let responseArray = urls.map(function(url) {
request.get(url, {
timeout: 5000
}, function(err, res, body) {
if (!err) {
console.log(`Response status from ${url} is ${res.statusCode}`);
return {
url: url,
status: res.statusCode
};
} else {
console.log(`Exception occurred while connecting to ${url} ERR:: ${err}`);
return {
url: url,
status: 0
};
}
});
});
console.log("This is responseArray: ", responseArray);
}
The log before my return statement is perfect and goes as:
Response status from https://google.com is 200
Response status from https://github.com is 200
But the final log gives undefined :
This is responseArray: [ undefined, undefined ]
Also the log giving undefined is printed first instead of Response status logs.
Edit As mentioned by @thomas in the comment I had also tried putting return exactly before request.get
but that returned me
This is responseArray: [ Request {
domain: null,
_events:
{ error: [Function: bound ],
complete: [Function: bound ],
pipe: [Function] },
_eventsCount: 3,
_maxListeners: undefined,
timeout: 5000,
uri:
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'google.com',
port: 443,
hostname: 'google.com',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'https://google.com/' },
callback: [Function],
method: 'GET',
readable: true,
writable: true,
explicitMethod: true,
_qs:
Querystring {
request: [Circular],
lib: [Object],
useQuerystring: undefined,
parseOptions: {},
stringifyOptions: {} },
_auth:
Auth {
request: [Circular],
hasAuth: false,
sentAuth: false,
bearerToken: null,
user: null,
pass: null },
_oauth: OAuth { request: [Circular], params: null },
_multipart:
Multipart {
request: [Circular],
boundary: 'e629bc7c-50e0-423f-a5d2-30038ac037e3',
chunked: false,
body: null },
_redirect:
Redirect {
request: [Circular],
followRedirect: true,
followRedirects: true,
followAllRedirects: false,
followOriginalHttpMethod: false,
allowRedirect: [Function],
maxRedirects: 10,
redirects: [],
redirectsFollowed: 0,
removeRefererHeader: false },
_tunnel:
Tunnel {
request: [Circular],
proxyHeaderWhiteList: [Object],
proxyHeaderExclusiveList: [] },
headers: { host: 'google.com' },
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'google.com',
path: '/',
httpModule:
{ Server: [Object],
createServer: [Function],
globalAgent: [Object],
Agent: [Object],
request: [Function],
get: [Function] },
agentClass: { [Function: Agent] super_: [Object] },
agent:
Agent {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: {},
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] } },
Request {
domain: null,
_events:
{ error: [Function: bound ],
complete: [Function: bound ],
pipe: [Function] },
_eventsCount: 3,
_maxListeners: undefined,
timeout: 5000,
uri:
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'github.com',
port: 443,
hostname: 'github.com',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'https://github.com/' },
callback: [Function],
method: 'GET',
readable: true,
writable: true,
explicitMethod: true,
_qs:
Querystring {
request: [Circular],
lib: [Object],
useQuerystring: undefined,
parseOptions: {},
stringifyOptions: {} },
_auth:
Auth {
request: [Circular],
hasAuth: false,
sentAuth: false,
bearerToken: null,
user: null,
pass: null },
_oauth: OAuth { request: [Circular], params: null },
_multipart:
Multipart {
request: [Circular],
boundary: 'e15018e6-6f56-4d42-9a0b-ff471b3e0995',
chunked: false,
body: null },
_redirect:
Redirect {
request: [Circular],
followRedirect: true,
followRedirects: true,
followAllRedirects: false,
followOriginalHttpMethod: false,
allowRedirect: [Function],
maxRedirects: 10,
redirects: [],
redirectsFollowed: 0,
removeRefererHeader: false },
_tunnel:
Tunnel {
request: [Circular],
proxyHeaderWhiteList: [Object],
proxyHeaderExclusiveList: [] },
headers: { host: 'github.com' },
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'github.com',
path: '/',
httpModule:
{ Server: [Object],
createServer: [Function],
globalAgent: [Object],
Agent: [Object],
request: [Function],
get: [Function] },
agentClass: { [Function: Agent] super_: [Object] },
agent:{...} and ...
I had also tried forEach as mentioned by renzo it gives me this:
This is responseArray: []
EDIT 2
As mentioned by thomas I also tried promise.all() which returned:
This is responseArray: [ undefined, undefined ]
Response status from https://google.com is 200
Response status from https://github.com is 200