I'm trying to understand how argument and variable handling are achieved for nested functions, in particular with VO.js, though maybe this applies to IIFE's in general? In the following example, id
seems to pass through without needing to be declared as an argument, but shopCat
does not pass through unless it is part of the arguments following the function definition. This appears inconsistent, though I'm sure it's something commonplace and simple. I apologize if my terminology is incorrect, as I am still learning :)
//Defined elsewhere and passed to pubID//
var id = '1000';
var shopCat = [array of product objects];
pubID(id,shopCat);
function pubID(id,shopCat){
console.log('outer',!!shopCat)
//Returns true //
return new Promise(function(resolve, reject) {
console.log('inner', !!shopCat)
//Returns true
vo(function*(shopCat){
//Must include shopCat as an argument, but not id? //
console.log(!!id)
//Returns true //
console.log('vo',!!shopCat);
//RETURNS FALSE UNLESS PASSED AS AN ARGUMENT//
var errorLog = [];
var mongo = yield Product.find({id: id});
if(!shopCat){
var shopCat = yield SC.getAllProducts();
}
var comps = yield Scraper.getComps();
var col = Scraper.collectProducts([id],mongo);
var addMetaFields = false;
var ts = SC.transposeProdArr(col,shopCat,comps, addMetaFields);
var ap = SC.assembleProductArr(ts, shopCat);
var trans = yield SC.transmitProducts(ap, errorLog);
var report = {
trans : trans,
eL : errorLog
};
console.log('done');
return report;
})(shopCat, function(e,d){resolve(d)})
//SHOPCAT MUST BE PASSED AS AN ARGUMENT, ID DOES NOT? //
});
}
The Context for shopCat::
function pubAllID(ids){
return new Promise(function(resolve, reject) {
vo(function*(){
var cats = yield {
shopCat : SC.getAllProducts(),
ids : Product.find({},'id'),
comps: Scraper.getComps()
};
var comps = cats.comps;
var shopCat = cats.shopCat;
ids = ids ? ids : us.chain(cats.ids).pluck('id').uniq().value();
console.log('ids.length:',ids.length);
var i = 0;
var trans = [];
var errorLog = [];
yield (function* nextID(){
if(i < ids.length){
console.log(i);
var pub = yield {
a: pubID(ids[i++],shopCat,comps),
b: pubID(ids[i++],shopCat,comps)
};
trans = trans.concat(pub.a.trans,pub.b.trans);
errorLog = errorLog.concat(pub.a.eL,pub.b.eL);
pub = []
if(i%50 == 0){
console.log("syncWithShopify done",v8.getHeapStatistics());
}
yield nextID();
}
})()...