var request = require("request").defaults({ encoding: null });
var picturesArray = [];
request.get('http://www.vorohome.com//images/assets/159314_887955.png', function (error, response, body) {
if (!error && response.statusCode == 200) {
data = "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');
console.log(data)
picturesArray[0] = data;
}
});
console.log(picturesArray);
Asked
Active
Viewed 39 times
0
-
@Armin — That's terrible advice. There's a callback function will fill fire when the data is available. Guessing how long it will take to become available is just stupid. – Quentin Jul 30 '18 at 11:33
-
But guys any suggestion how I can collect the data in pictureArray? – Kunal Jul 30 '18 at 11:35
-
See the duplicate question linked in the big yellow box at the top of the page. – Quentin Jul 30 '18 at 11:39
1 Answers
1
Because you're calling
console.log(picturesArray);
before asynchronous action is finished.
If you'll add this log inside the request callback - you'll get the proper result.

hsz
- 148,279
- 62
- 259
- 315
-
-
1@Armin — No! Absolutely not! Guessing how long an asynchronous function will take to resolve is stupid. Just use the callback feature which that function provides to tell you when it is done. – Quentin Jul 30 '18 at 11:31
-
-
-