@Eldar, that's an iterator function not a callback. A callback is very
specifically a function (err, value) {} that will not be called
synchronously.

@Sigurgeir Webworkers do IO. The message passing is cross thread
communication which is IO.

My stance on using process.nextTick to time slice large array computation
is your doing it wrong. If you want to do an expensive computation over a
large set don't load the entire set into memory and do it in a blocking
fashion. Instead create a Readable stream for the large amount of data and
then use a Transform stream to do the transformation piecewise. Thus you
avoid high memory cost and large blocking computation cost.

So basically only use callbacks for asynchronous operations.


On Tue, Apr 9, 2013 at 8:48 PM, Sigurgeir Jonsson <
[email protected]> wrote:

> As javascript is single threaded you usually don't need callback unless
> it's IO related.  However, there are exceptions:
>   (i) Heavy calculations might want to utilize process.nextTick() or
> timeout, to avoid blocking.   You would have to use a callback to return
> the final results
>   (ii) Webworker results are passed as messages (not a return statement)
> so inherently asynchronous, even when there is no "real" IO.
>
>
> On Tuesday, April 9, 2013 7:10:12 PM UTC-4, Eldar wrote:
>
>> No, you get it all wrong.
>>
>> Callbacks you use for `for-loops` (array.forEach() I guess) are not async
>> at all.
>>
>> The forEach() method is actually nothing but:
>>
>> // simplified
>> Array.prototype.forEach = function (cb) {
>>   for (var i = 0; i < this.length; i++) {
>>     cb(this[i])
>>   }
>> }
>>
>> So it just performs iterating stuff for you and frees your hands from
>> bothering with for loop.
>>
>> I strongly recommend you to stop writing any code and read good book
>> about javaScript and
>> functional programming in general.
>>
>> On Wednesday, April 10, 2013 2:02:44 AM UTC+4, Mil Werns wrote:
>>>
>>> I have a simple question: When should I use callbacks?
>>>
>>> Currently, I'm only using callbacks in I/O cases (file, database and
>>> network access).
>>> My application has to iterate over a lot of big arrays and combines this
>>> data to new objects. I'm doing this with a standard (synchronous) for-loop,
>>> because this task needs only CPU power. So there would be no benefit to use
>>> an asynchronous call.
>>>
>>> Is this assumption correct?
>>> Can you please give me other examples for useful callbacks other than
>>> those mentioned above?
>>>
>>> Thank you
>>>
>>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to