Как рекурсивно yield-ить генераторы
Всем привет!
Не получается рекурсивно за-yield-ить генератор причём еще в цикле. Использую Koa и CO Ругается на строку yield* gen(params.src + '/' + child, (params.obj ? params.obj[shortName] : hierarchies[shortName])); Promise is not function 'use strict'; const fs = require('fs'); const hierRoot = require('config').hierarchiesRoot; const parseString = require('xml2js').parseString; const config = require('config'); var co = require('co'); var path = require('path'); var hierarchies = {}; function checkForXml(src, obj) { return new Promise(function (resolve, reject) { fs.readdir(src, function (err, data) { if (err) throw err; if (data.indexOf('properties.xml') != -1) { let params = { src: src, obj: obj }; resolve(params); } }); }); } function readXml(params) { return new Promise(function (resolve, reject) { let xmlData = fs.readFile(params.src + '/properties.xml', function (err, data) { if (err) throw err; params.xml = data; resolve(params); }); }); } function parseXml(params) { return new Promise(function (resolve, reject) { parseString(params.xml, function (err, result) { // парсим xml if (err) throw err; if (!result['SPEC-HIERARCHY']['$']['SHORT-NAME']) { throw new Error('SPEC-HIERARCHY SHORT-NAME if not defined !!!'); } let shortName = result['SPEC-HIERARCHY']['$']['SHORT-NAME']; if (params.obj) { // on first iteration it must be NULL params.obj[shortName] = {}; } else { hierarchies[shortName] = {}; } if (result['SPEC-HIERARCHY']['CHILD']) { let shortNameChilds = childHir(result['SPEC-HIERARCHY']['CHILD']); params.childs = shortNameChilds; resolve(params); } }); }); } function childHir(childs) { let arr = []; childs.forEach(function (child) { arr.push(child['$']['SHORT-NAME']); }); return arr; } function* gen (src, hierObj) { let params = yield checkForXml((src ? src : hierRoot), (hierObj ? hierObj : null)); params = yield readXml(params); params = yield parseXml(params); if (params.childs || params.childs.length > 0) { params.childs.forEach(co(function* (child) { yield* gen(params.src + '/' + child, (params.obj ? params.obj[shortName] : hierarchies[shortName])); })); } } module.exports = function* (next) { yield* gen; hierarchies = JSON.stringify(hierarchies) yield this.render('index', { hierarchies : hierarchies}); }; |
Часовой пояс GMT +3, время: 09:49. |