Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
add stacktrace on safe, and notes on curry
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed Sep 9, 2011
1 parent 3d38a88 commit c78c367
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
6 changes: 4 additions & 2 deletions async.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function tryCatchPass (_try,_catch,_pass) {
exports.safe = safe

function safe (funx) {
var err = new Error((funx.name || funx.toString().slice(0,100) + '...') + ' called more than once')
return function () {
var _callback = arguments[arguments.length - 1]
, n = 0
Expand All @@ -82,7 +83,8 @@ function safe (funx) {
if(!n++)
_callback.apply(this,args)
else
console.log('callback function ' + _callback.name + ' called:' + n + ' times')
console.err(err.stack)
//console.log('callback function ' + _callback.name + ' called:' + n + ' times')
}
try {
funx.apply(null, arguments)
Expand Down Expand Up @@ -118,9 +120,9 @@ exports.timeout = function (func, time) {
checker(err)
}, time)
function checker () {
clearTimeout(timer)
if(! called ++)
callback.apply(null, [].slice.call(arguments))
clearTimeout(timer)
}
args.push(checker)
return func.apply(this, args)
Expand Down
36 changes: 36 additions & 0 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,43 @@ var objects = require('./objects')
exports.id = function (i) {
return i
}
/*
often you want to do something like: map(array, JSON.stringify)
but map calls the iterator with (value, key, object)
which confuses JSON.stringify because it expects a replacer function as the second arg
previously one would do something like this:
map(array, function (x) {return JSON.stringify(x)})
TOO LONG, I can't be bothered!
now use:
map(array, curry(JSON.stringify, 0))
the `0` that thu first arg is in the first position.
non numbers are literal, also, negative numbers count from the end of the array
(handy for ensuring that callbacks are in the right place)
curry(func, 0) // function (a) { return func(a) }
curry(func, 0, 'whatever') // function (a) { return func(a, 'whatever') }
curry(func, 0, 'whatever', -1) // function (a, b) { return func(a, 'whatever', b) }
of course, you cannot use this function to stick in numbers, but what are you, an accountant?
it's really handy though, when you can simplify this:
ctrl.toAsync(function (ls) {
return d.map(ls, function (e) { return path.join(dir, e) } )
}) ]) (dir, cb)
to this:
ctrl.toAsync(d.curry(d.map, 0, d.curry(path.join, dir, 0) ))
*/

exports.curry = function (/*funx, args...*/) {
var args = [].slice.call(arguments)
, funx = args.shift()
Expand Down

0 comments on commit c78c367

Please sign in to comment.