Path: csiph.com!aioe.org!news.mb-net.net!open-news-network.org!.POSTED.208.232.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Iterating through an array (special syntax) Date: Thu, 19 Oct 2017 21:53:11 +0200 Organization: PointedEars Software (PES) Lines: 97 Message-ID: <1876626.bB369e8A3T@PointedEars.de> References: <7f583c52-91a8-4773-a2cb-bc8534a1ad78@googlegroups.com> <22557651-daf7-4252-b3a4-c9df083be85c@googlegroups.com> <526590a8-1952-4da6-a8cd-525eef79df6f@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="208.232.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.232.208"; logging-data="12091"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.2 X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Now I have found a solution by myself (old fashion syntax): > > //===================================================== > function foo() { > var fn_done, fn_each; > var ctx = { > done:function(fn) {fn_done = fn; return this;}, > each:function(fn) {fn_each = fn; return this;} > } > setTimeout(function() { > var oldItems = [1,2,3,4], > newItems = oldItems.map(function(x) {return x * 2}); > if (typeof fn_each == 'function') { > for (var i=0; i fn_each(oldItems[i], newItems[i]); > } This loop is pointlessly inefficient in any case. You are already using Array.prototype.map(), thereby iterating through the array, so you can either output or accumulate the old and new value *in the callback*. > } > if (typeof fn_done == 'function') fn_done(newItems); > }, 500); > return ctx; > } > > console.log('------------------------------'); > foo() > .each(function(oldItem, newItem) { > console.log('oldItem: ' + oldItem + ' ' + 'newItem: ' + newItem); > }) > .done(function(result) { > console.log('------------------------------'); > console.log('result: ' + result); > console.log('------------------------------'); > }); > //===================================================== > > I am sure this can be more elegant but it seems to work. In general that approach is correct. However, in Node.js 4.8+ and other conforming implementations of ECMAScript Ed. 6+, it is not necessary anymore to write all that custom, little-tested code for implementing a well-known design pattern: var oldItems = Array.apply(null, {length: 10}).map( function (e, i) { return i; }); var foo = function(oldItem, newItem) { console.log('oldItem: ' + oldItem + ',', 'newItem: ' + newItem); }; new Promise(function (resolve, reject) { setTimeout(function() { var newItems = oldItems.map(function (item) { var newItem = item * 2; foo(item, newItem); return newItem; }); resolve(newItems); }, 500); }).then(function (result) { /* Use window.alert() for testing in the Chromium console instead */ console.log(result); }); It should be other other way around anyway: whatever the setTimeout() call is replaced with should provide a way to pass the success and failure callback by implementing a Promise-like pattern, just like the original setTimeout *already* *does* (but only for success). Code that can reasonably be abstracted away from the caller should be abstracted away. For example, instances of XMLHTTPRequest() now have an “onload” and “onerror” property, and the new, even more compact Fetch API (not available in Node.js 4.8.2) returns Promises. There is no best one-size-fits-all solution, and chaining is not everything. Do not fall into the jQuery trap. (Told you so.) -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.