Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34118 > unrolled thread
| Started by | Rolf Niepraschk <Rolf.Niepraschk@gmx.de> |
|---|---|
| First post | 2017-10-17 12:39 +0200 |
| Last post | 2017-10-18 04:50 +0200 |
| Articles | 20 — 5 participants |
Back to article view | Back to comp.lang.javascript
Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-17 12:39 +0200
Re: Iterating through an array (special syntax) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-10-17 05:54 -0700
Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-17 15:03 +0200
Re: Iterating through an array (special syntax) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-10-17 06:19 -0700
Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-17 18:46 +0200
Re: Iterating through an array (special syntax) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-18 13:55 +0200
Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-18 14:25 +0200
Re: Iterating through an array (special syntax) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-10-18 15:38 +0200
Re: Iterating through an array (special syntax) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-18 15:49 +0200
Re: Iterating through an array (special syntax) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-10-18 16:25 -0700
Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-19 07:50 +0200
Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-19 15:15 +0200
Re: Iterating through an array (special syntax) Julio Di Egidio <julio@diegidio.name> - 2017-10-19 06:36 -0700
Re: Iterating through an array (special syntax) Julio Di Egidio <julio@diegidio.name> - 2017-10-19 06:39 -0700
Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-19 18:16 +0200
Re: Iterating through an array (special syntax) Julio Di Egidio <julio@diegidio.name> - 2017-10-19 18:55 -0700
Re: Iterating through an array (special syntax) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-19 21:53 +0200
Re: Iterating through an array (special syntax) Rolf Niepraschk <Rolf.Niepraschk@gmx.de> - 2017-10-20 07:55 +0200
Re: Iterating through an array (special syntax) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-20 21:48 +0200
Re: Iterating through an array (special syntax) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-18 04:50 +0200
| From | Rolf Niepraschk <Rolf.Niepraschk@gmx.de> |
|---|---|
| Date | 2017-10-17 12:39 +0200 |
| Subject | Iterating through an array (special syntax) |
| Message-ID | <f4m4ugF4q8tU1@mid.individual.net> |
Hello,
I search for a function with the following characteristics:
--------------------------
function foo(arr) {
/* unknown implementation */
}
Call:
foo([1,2,3,4])
.each(function(oldItem, newItem) {
console.log('old: ' + oldItem + ' ' + 'new: ' + newItem);
// should be:
// old: 1 new: 2
// old: 2 new: 4
// old: 3 new: 6
// old: 4 new: 8
})
.done(function(result) {
console.log('Result: ' + result);// should be: [2,4,6,8]
}
--------------------------
The names "each" and "done" are not important. The static array is an
example. In reality the array is the result of a network request inside
of the function.
What would be the best implementation? Thanks in advance.
...Rolf
[toc] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2017-10-17 05:54 -0700 |
| Message-ID | <7f583c52-91a8-4773-a2cb-bc8534a1ad78@googlegroups.com> |
| In reply to | #34118 |
Rolf Niepraschk wrote:
> Hello,
>
> I search for a function with the following characteristics:
> --------------------------
> function foo(arr) {
> /* unknown implementation */
> }
>
> Call:
>
> foo([1,2,3,4])
> .each(function(oldItem, newItem) {
> console.log('old: ' + oldItem + ' ' + 'new: ' + newItem);
> // should be:
> // old: 1 new: 2
> // old: 2 new: 4
> // old: 3 new: 6
> // old: 4 new: 8
> })
> .done(function(result) {
> console.log('Result: ' + result);// should be: [2,4,6,8]
> }
> --------------------------
> The names "each" and "done" are not important. The static array is an
> example. In reality the array is the result of a network request inside
> of the function.
>
> What would be the best implementation? Thanks in advance.
Standard JavaScript functionalty:
[1,2,3,4].map(i => i * 2)
[toc] | [prev] | [next] | [standalone]
| From | Rolf Niepraschk <Rolf.Niepraschk@gmx.de> |
|---|---|
| Date | 2017-10-17 15:03 +0200 |
| Message-ID | <f4mddoF6nddU1@mid.individual.net> |
| In reply to | #34119 |
Am 17.10.2017 um 14:54 schrieb Michael Haufe (TNO): ... > > > Standard JavaScript functionalty: > > [1,2,3,4].map(i => i * 2) > And how can I implement the functions "each" and "done" (see my example)? ...Rolf
[toc] | [prev] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2017-10-17 06:19 -0700 |
| Message-ID | <22557651-daf7-4252-b3a4-c9df083be85c@googlegroups.com> |
| In reply to | #34120 |
Rolf Niepraschk wrote:
> Michael Haufe (TNO):
> ...
> >
> >
> > Standard JavaScript functionalty:
> >
> > [1,2,3,4].map(i => i * 2)
> >
>
> And how can I implement the functions "each" and "done" (see my example)?
This can be a bit more efficient by eliminating the intermediate object and such, but I prefer clarity in this case as we don't know the larger architectural requirement:
<script>
function foo(xs) {
return {
oldItems: xs,
newItems: xs.map(x => x * 2),
each: function(fnUnzip){
xs.forEach((x,i) => fnUnzip(x, this.newItems[i]))
}
}
}
foo([1,2,3,4]).each((oldItem,newItem) => {
console.log(`oldItem: ${oldItem} newItem: ${newItem}`)
})
</script>
This is newer than ES5 so compile/translate as necessary for your target environment.
[toc] | [prev] | [next] | [standalone]
| From | Rolf Niepraschk <Rolf.Niepraschk@gmx.de> |
|---|---|
| Date | 2017-10-17 18:46 +0200 |
| Message-ID | <f4mqfvF9pi1U1@mid.individual.net> |
| In reply to | #34121 |
Am 17.10.2017 um 15:19 schrieb Michael Haufe (TNO):
...
>
> This can be a bit more efficient by eliminating the intermediate object and such, but I prefer clarity in this case as we don't know the larger architectural requirement:
>
> <script>
> function foo(xs) {
> return {
> oldItems: xs,
> newItems: xs.map(x => x * 2),
> each: function(fnUnzip){
> xs.forEach((x,i) => fnUnzip(x, this.newItems[i]))
> }
> }
> }
>
> foo([1,2,3,4]).each((oldItem,newItem) => {
> console.log(`oldItem: ${oldItem} newItem: ${newItem}`)
> })
> </script>
>
> This is newer than ES5 so compile/translate as necessary for your target environment.
>
This solution is very helpful and I can use modern browsers and a recent
nodejs version. Many Thanks.
But one Problem remains. How must be the function modified if the array
is generated in an asynchronous way? For example:
//--------------------------------------------------------
function foo() {
setTimeout(function() {
var arr = [1,2,3,4];
// ???
}, 500);
}
foo().each((oldItem,newItem) => {
console.log(`oldItem: ${oldItem} newItem: ${newItem}`)
})
//--------------------------------------------------------
...Rolf
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-10-18 13:55 +0200 |
| Message-ID | <8167474.CDJkKcVGEf@PointedEars.de> |
| In reply to | #34122 |
Rolf Niepraschk wrote:
> […] How must be the function modified if the array
> is generated in an asynchronous way? For example:
>
> //--------------------------------------------------------
> function foo() {
> setTimeout(function() {
> var arr = [1,2,3,4];
> // ???
> }, 500);
> }
>
> foo().each((oldItem,newItem) => {
> console.log(`oldItem: ${oldItem} newItem: ${newItem}`)
> })
> //--------------------------------------------------------
function foo (callback)
{
var t = window.setTimeout(function () {
window.clearTimeout(t);
var arr = [1, 2, 3, 4];
callback(arr);
}, 500);
}
foo((a) => {
// a.forEach(…);
});
But AISB, your approach at problem solving is *unwise* at best.
For example, window.setTimeout() is a host object’s method provided by Web
browsers; it is unavailable in nodeJS, while there are other, *better* ways
to do this in nodeJS, *depending on the specific task*.
[en] <https://en.wikipedia.org/wiki/Problem_solving>
[de] <https://de.wikipedia.org/wiki/Probleml%C3%B6sen>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Rolf Niepraschk <Rolf.Niepraschk@gmx.de> |
|---|---|
| Date | 2017-10-18 14:25 +0200 |
| Message-ID | <f4ovh0Fote6U1@mid.individual.net> |
| In reply to | #34124 |
Am 18.10.2017 um 13:55 schrieb Thomas 'PointedEars' Lahn:
> Rolf Niepraschk wrote:
>
>> […] How must be the function modified if the array
>> is generated in an asynchronous way? For example:
>>
>> //--------------------------------------------------------
>> function foo() {
>> setTimeout(function() {
>> var arr = [1,2,3,4];
>> // ???
>> }, 500);
>> }
>>
>> foo().each((oldItem,newItem) => {
>> console.log(`oldItem: ${oldItem} newItem: ${newItem}`)
>> })
>> //--------------------------------------------------------
>
> function foo (callback)
> {
> var t = window.setTimeout(function () {
> window.clearTimeout(t);
> var arr = [1, 2, 3, 4];
> callback(arr);
> }, 500);
> }
>
> foo((a) => {
> // a.forEach(…);
> });
>
This is _not_ what I want. I want a syntax similar to JQuery e.g.
$.get(...)
.done(...)
.fail(...)
> But AISB, your approach at problem solving is *unwise* at best.
Please accept my wishes or do not write here.
>
> For example, window.setTimeout() is a host object’s method provided by Web
> browsers; it is unavailable in nodeJS, while there are other, *better* ways
"setTimeout()" _is_ available in nodeJS and in my example it is only a
place holder of any asynchronous way for creating an array.
...Rolf
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-10-18 15:38 +0200 |
| Message-ID | <XnsA8129F147D35eejj99@194.109.6.166> |
| In reply to | #34125 |
Rolf Niepraschk <Rolf.Niepraschk@gmx.de> wrote on 18 Oct 2017 in comp.lang.javascript: > his is _not_ what I want. [..] > Please accept my wishes or do not write here. Eh, this is Usenet, NOT a helpdesk, paid or unpaid. You should not limit the scope of responses to a posting. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-10-18 15:49 +0200 |
| Message-ID | <3832921.ejJDZkT8p0@PointedEars.de> |
| In reply to | #34125 |
Rolf Niepraschk wrote:
> Am 18.10.2017 um 13:55 schrieb Thomas 'PointedEars' Lahn:
>> function foo (callback)
>> {
>> var t = window.setTimeout(function () {
>> window.clearTimeout(t);
>> var arr = [1, 2, 3, 4];
>> callback(arr);
>> }, 500);
>> }
>>
>> foo((a) => {
>> // a.forEach(…);
>> });
>
> This is _not_ what I want. I want a syntax similar to JQuery e.g.
>
> $.get(...)
> .done(...)
> .fail(...)
I have given you enough clues. Do your own homework.
>> But AISB, your approach at problem solving is *unwise* at best.
>
> Please accept my wishes or do not write here.
You must be kidding. Usenet is _not_ what *you* *want*.
<https://tty1.net/smart-questions_de.html>
>> For example, window.setTimeout() is a host object’s method provided by
>> Web browsers; it is unavailable in nodeJS, while there are other,
>> *better* ways
>
> "setTimeout()" _is_ available in nodeJS
I stand corrected:
<https://nodejs.org/api/timers.html>
<https://nodejs.org/en/docs/guides/timers-in-node/>
However, as one can see there, it works slightly differently in Node.js than
in Web UAs.
> and in my example it is only a place holder of any asynchronous way for
> creating an array.
I was aware of that.
Score adjusted
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2017-10-18 16:25 -0700 |
| Message-ID | <526590a8-1952-4da6-a8cd-525eef79df6f@googlegroups.com> |
| In reply to | #34122 |
Rolf Niepraschk wrote:
> schrieb Michael Haufe (TNO):
> ...
> This solution is very helpful and I can use modern browsers and a recent
> nodejs version. Many Thanks.
>
> But one Problem remains. How must be the function modified if the array
> is generated in an asynchronous way? For example:
>
> //--------------------------------------------------------
> function foo() {
> setTimeout(function() {
> var arr = [1,2,3,4];
> // ???
> }, 500);
> }
>
> foo().each((oldItem,newItem) => {
> console.log(`oldItem: ${oldItem} newItem: ${newItem}`)
> })
> //--------------------------------------------------------
>
I think we need more info for a proper solution. Is it always going to be async? or is it going to be both? or something else?
[toc] | [prev] | [next] | [standalone]
| From | Rolf Niepraschk <Rolf.Niepraschk@gmx.de> |
|---|---|
| Date | 2017-10-19 07:50 +0200 |
| Message-ID | <f4qspeF7t7gU1@mid.individual.net> |
| In reply to | #34128 |
Am 19.10.2017 um 01:25 schrieb Michael Haufe (TNO):
> Rolf Niepraschk wrote:
>> schrieb Michael Haufe (TNO):
>> ...
>
>> This solution is very helpful and I can use modern browsers and a recent
>> nodejs version. Many Thanks.
>>
>> But one Problem remains. How must be the function modified if the array
>> is generated in an asynchronous way? For example:
>>
>> //--------------------------------------------------------
>> function foo() {
>> setTimeout(function() {
>> var arr = [1,2,3,4];
>> // ???
>> }, 500);
>> }
>>
>> foo().each((oldItem,newItem) => {
>> console.log(`oldItem: ${oldItem} newItem: ${newItem}`)
>> })
>> //--------------------------------------------------------
>>
>
> I think we need more info for a proper solution. Is it always going to be async? or is it going to be both? or something else?
>
Always async. Sorry for the missing information.
...Rolf
[toc] | [prev] | [next] | [standalone]
| From | Rolf Niepraschk <Rolf.Niepraschk@gmx.de> |
|---|---|
| Date | 2017-10-19 15:15 +0200 |
| Message-ID | <f4rmr3Fdt6iU1@mid.individual.net> |
| In reply to | #34128 |
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<oldItems.length; i++) {
fn_each(oldItems[i], newItems[i]);
}
}
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.
...Rolf
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2017-10-19 06:36 -0700 |
| Message-ID | <0ea7227f-1148-4bb2-b398-0fdf7f0613d9@googlegroups.com> |
| In reply to | #34132 |
On Thursday, October 19, 2017 at 3:15:26 PM UTC+2, Rolf Niepraschk wrote:
> 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<oldItems.length; i++) {
> fn_each(oldItems[i], newItems[i]);
> }
> }
> 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.
Well, it's more than just little elegance the fact that you have to set a
timeout in order not to pass two callbacks to the constructor...
Julio
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2017-10-19 06:39 -0700 |
| Message-ID | <9c420447-dc3f-4b07-8592-4cb8dabc70ca@googlegroups.com> |
| In reply to | #34133 |
On Thursday, October 19, 2017 at 3:36:21 PM UTC+2, Julio Di Egidio wrote:
> On Thursday, October 19, 2017 at 3:15:26 PM UTC+2, Rolf Niepraschk wrote:
> > 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<oldItems.length; i++) {
> > fn_each(oldItems[i], newItems[i]);
> > }
> > }
> > 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.
>
> Well, it's more than just little elegance the fact that you have to set a
> timeout in order not to pass two callbacks to the constructor...
To the function...
Julio
[toc] | [prev] | [next] | [standalone]
| From | Rolf Niepraschk <Rolf.Niepraschk@gmx.de> |
|---|---|
| Date | 2017-10-19 18:16 +0200 |
| Message-ID | <f4s1emFgcajU1@mid.individual.net> |
| In reply to | #34133 |
Am 19.10.2017 um 15:36 schrieb Julio Di Egidio: ... >> >> I am sure this can be more elegant but it seems to work. > > Well, it's more than just little elegance the fact that you have to set a > timeout in order not to pass two callbacks to the constructor... > I repeat: the timeout is only a place holder. Read the whole story. I like the syntax of jquery and other ... ...Rolf
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2017-10-19 18:55 -0700 |
| Message-ID | <6bea41db-ace4-477a-a7b9-0a61257c81c4@googlegroups.com> |
| In reply to | #34135 |
On Thursday, October 19, 2017 at 6:16:33 PM UTC+2, Rolf Niepraschk wrote: > Am 19.10.2017 um 15:36 schrieb Julio Di Egidio: > ... > >> I am sure this can be more elegant but it seems to work. > > > > Well, it's more than just little elegance the fact that you have to set a > > timeout in order not to pass two callbacks to the constructor... > > I repeat: the timeout is only a place holder. Read the whole story. I > like the syntax of jquery and other ... I know what you said: your solution is not modular and in that quite ugly. Julio
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-10-19 21:53 +0200 |
| Message-ID | <1876626.bB369e8A3T@PointedEars.de> |
| In reply to | #34132 |
Rolf Niepraschk wrote:
> 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<oldItems.length; i++) {
Unless oldItems.length changes,
for (var i=0, len = oldItems.length; i < len; i++) {
is more efficient.
> 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: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Rolf Niepraschk <Rolf.Niepraschk@gmx.de> |
|---|---|
| Date | 2017-10-20 07:55 +0200 |
| Message-ID | <f4thebFqrssU1@mid.individual.net> |
| In reply to | #34141 |
Am 19.10.2017 um 21:53 schrieb Thomas 'PointedEars' Lahn:
...
>
> Unless oldItems.length changes,
>
> for (var i=0, len = oldItems.length; i < len; i++) {
>
> is more efficient.
Are you sure? I see no much differences (at least with nodejs 8):
#=================================================
#!/usr/bin/env node
var arr = [];
for(var i=0; i<5E7; i++) arr.push(i);
var x, start;
start = new Date().getTime();
for (var i=0; i<arr.length; i++) {};
console.log('A: ' + (new Date().getTime()-start) + ' ms');
start = new Date().getTime();
for (var i=0, len = arr.length; i < len; i++) {};
console.log('B: ' + (new Date().getTime()-start) + ' ms');
#=================================================
>
>> 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*.
Ok.
I will think about your Promise solution. Thanks for your explanation.
...Rolf
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-10-20 21:48 +0200 |
| Message-ID | <1553669.BRYDE4hZ6a@PointedEars.de> |
| In reply to | #34150 |
Rolf Niepraschk wrote:
> Am 19.10.2017 um 21:53 schrieb Thomas 'PointedEars' Lahn:
>> Unless oldItems.length changes,
>>
>> for (var i=0, len = oldItems.length; i < len; i++) {
>>
>> is more efficient.
>
> Are you sure?
Yes. It is most obvious in the DOM.
> I see no much differences (at least with nodejs 8):
> [test with an *empty* loop over only 100'000 elements]
That does not mean anything; for example, sometimes internal optimization
will take place, too. Your empty loops are prime candidates for internal
optimization to begin with.
My statement is true in general (as a rule of thumb), and if you think more
carefully about it (i.e., how the “for” command works), you will realize
why.
> I will think about your Promise solution. Thanks for your explanation.
You’re welcome.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-10-18 04:50 +0200 |
| Message-ID | <3358688.kQq0lBPeGt@PointedEars.de> |
| In reply to | #34118 |
Rolf Niepraschk wrote: > I search for a function with the following characteristics: > […] > What would be the best implementation? This is a question often asked by beginners, and it is never a question that you should be asking (yourself). By asking such a question you only invite suggestions based on subjective instead of objective reasons. Because there is never only one best implementation. Each implementation has its advantages and disadvantages, and different people have different preferences. People can be wrong in their preferences, and you do not want to carry the problems of their misjudgement just because you have not thought carefully enough about a problem. First you need to define (for yourself) *what* you want to achieve, then you can ask (yourself) about *ways* to do it, and only then you can reasonably decide on what is *best for you in the respective specific situation*. Not the other way around. -- PointedEars FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix> <https://github.com/PointedEars> | <http://PointedEars.de/wsvn/> Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web