0

enter code hereI have the following code

function a(){alert("a");}

I want to create a function b as

function b(){alert("a"); alert("b");}

My approach is something like

var b = a + alert("b");

This is of course not working. But I am wondering if there is some kind of library supporting this.

Edit: Maybe I need to describe my scenario so that its more clear what I want to achieve.

I am using async.js library to handler multiple async calls. My code looks like

var values = {};
...
function all() {
    var allDfd = $.Deferred();
    async.parallel(
            [function (callback) {
                remoteCall(function (result) {
                    values.v1 = result;
                    callback(null, 'one');
                });
            },
            function (callback) {
                remoteCall(function (result) {
                    values.v2 = result;
                    callback(null, "two");
                });
            },
            function (callback) {

                remoteCall(function (result) {

                    values.v3 = result;
                    callback(null, "three");
                });
            }], function (err, results) {
                allDfd.resolve();
            });
    return allDfd.promise();
}

Clearly there are a lot of repetitive code that bothers me. So my idea is to create a function asyncCall to perform the boilerplate tasks. The idea is

   var values = {};
...
function all() {
    var allDfd = $.Deferred();
    function getAsyncCall (func, innerCallback, callback) {
        return function asyncCall(func, innnerCallback, callback){
            func(innerCallback + callback(null));  // combine innerCallBack and callback code
        }
    }

    async.parallel(
            [getAsyncCall(remoteCall, function(result){values.v1=result;},callback),
             getAsyncCall(remoteCall, function(result){values.v2=result;},callback),
             getAsyncCall(remoteCall, function(result){values.v3=result;},callback),
            ], function (err, results) {
                allDfd.resolve();
            });
    return allDfd.promise();
}

The line with the comment is what I am pondering. I am trying to create a new function that combines inner and outer callbacks.

2 Answers 2

4

You can do

var b = function() { a(); alert('b'); }
Sign up to request clarification or add additional context in comments.

Comments

1

You could write:

var a=function(){alert("a");}

var b=function(){a(); alert("b");}

And to go a little further, you can even write a whole function composition function:

function compose( functions ) {
   return function(){ 
      for(var i=0; i!=functions.length; ++i) {
        functions[i]();
      }
   };
}

var c=compose( [a, function(){ alert("b"); }] );

(See it at work at http://jsfiddle.net/xtofl/Pdrge/)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.