2016-11-10 105 views
0

我使用Elasticsearch JS客户端和节点来学习ES和Javascript。我使用JavaScript构建系统SublimeText2这样定义的运行JS代码:等待回调在节点中完成

{ 
    "cmd": ["C:\\Program Files\\nodejs\\node.exe", "$file"], 
    "selector": "source.js" 
} 

写到这将数据提交到ES索引:

"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/'); 

数据来源:https://github.com/dariusk/corpora/blob/master/data/humans/us_presidents.json

输出:

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 } 
... 

问题:

  1. 为什么打印results输出空数组,但问题是我怎么能等待这些回调完成很明显? (我不是说同步等待,而是以异步回调方式)。它可能可以用承诺来完成,但我还没有学过承诺,现在想学习如何做这个“回调”的方式。

  2. 是否有某种方法可以使JSON对象上的字符串连接不会像[object Object]那样得到表示,而是使用对象字面量? (如果我打电话console.log(obj)我得到对象字面值的字符串表示,而不是这个[object Object]“简写”)。使用.toString()是不行的。

回答

1
  1. async提供异步的原语/工作流程API用于处理使用基于回调方法异步请求。异步为集合上的performing异步操作提供了几乎1的1种方式。

    他们的模型是每个操作都通过回调。操作完成(成功或错误)操作调用回调。异步允许您注册一个回调,以便在所有操作完成时执行。

您可以通过跟踪您需要执行多少个异步操作以及完成时间来自定义操作。

var numOps = content.objects.length;

然后在回调你可以检查,如果这是要执行的最后一个回调。

function result_cb(err, resp) { 
    results.push({error:err, response:resp}); 
    if (results.length === numOps) { 
     // all operations have succeeded! `results` is completed 
    } 
} 

如果你看一下async源代码,他们都在做类似的事情,以保持异步状态。

  • 您可以创建自定义格式功能,或者您也可以登录该对象使用内置的标准库函数:https://stackoverflow.com/a/10729284/594589