Reputation: 6758
I'm using Elasticsearch JS client and node to learn ES and Javascript. I'm using Javascript build system in SublimeText2 defined like this to run the JS code:
{
"cmd": ["C:\\Program Files\\nodejs\\node.exe", "$file"],
"selector": "source.js"
}
Wrote this to submit data to ES for indexing:
"use strict";
const es = require('elasticsearch');
const path = require('path');
const fs = require('fs');
function es_connect(url, loglevel) {
if (typeof loglevel === 'undefined') { // somehow default function params feature from ES6 is not available in installable node/js
loglevel == 'error';
}
return new es.Client({host: url, log:loglevel});
}
function get_content(fname) {
const raw = fs.readFileSync(path.join('data', fname));
const content = JSON.parse(raw);
console.log('Found ' + content.objects.length + ' objects.');
return content;
}
function index_json_list(fname, index, doc_type, url) {
var content = get_content(fname);
var client = es_connect(url);
var results = [];
function result_cb(err, resp) {
console.log('Pushing error ' + err + ' and response ');
console.log(resp);
results.push({error:err, response:resp});
};
content.objects.forEach(function(x) {
console.log('___Submitting ');
console.log(x);
client.index({
index: index,
type: doc_type,
body: x
},
result_cb);
});
results.forEach(function(x){
console.log('indexing result: ' + x);
})
console.log('results');
console.log(results);
}
index_json_list('us_presidents.json', 'officials', 'president', 'http://localhost:9200/');
Data source: https://github.com/dariusk/corpora/blob/master/data/humans/us_presidents.json
Output:
Found 66 objects.
___Submitting
{ website: '',
startdate: '2009-01-20',
role_type_label: 'President',
....
leadership_title: null }
results
[]
Pushing error undefined and response
{ _index: 'officials',
_type: 'president',
_id: 'AVhOXERCNHzrCLGOfUu1',
_version: 1,
result: 'created',
_shards: { total: 2, successful: 1, failed: 0 },
created: true }
Pushing error undefined and response
{ _index: 'officials',
_type: 'president',
_id: 'AVhOXERBNHzrCLGOfUu0',
_version: 1,
result: 'created',
_shards: { total: 2, successful: 1, failed: 0 },
created: true }
...
Questions:
It's obvious why printing results
outputs empty array, but the question is how can I wait for those callbacks to complete? (I don't mean waiting synchronously, but rather in async callback manner). It can probably be done using promises, but I have not learned promises yet and for now want to learn how to do this "callback" way.
Is there some way to make string concatenation on JSON object not to get the representation like [object Object]
but instead use object literal? (if I call console.log(obj)
I get string representation of object literal, not this [object Object]
"shorthand"). Using .toString()
is no good.
Upvotes: 0
Views: 373
Reputation: 55962
async
provides asynchronous primitives/workflow api for handling asynchronous requests using callback based approach. async provides almost 1 for 1 way of performing async operations on collections.
Their model is that each operation is passed a callback. When the operation completes (either successful or error) that operation calls the callback. async allows you to register a callback to execute for when all operations have completed.
You could roll your own by keeping track of how many async operations you need to perform and when they are complete.
var numOps = content.objects.length;
then in the callback you could check if this is the last callback to be executed.
function result_cb(err, resp) {
results.push({error:err, response:resp});
if (results.length === numOps) {
// all operations have succeeded! `results` is completed
}
}
If you look at the async
source code they are doing a similar thing to maintain the async states.
Upvotes: 1