OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Execute array of callbacks with parameters [duplicate]

  • Thread starter Thread starter ProtocolGuy
  • Start date Start date
P

ProtocolGuy

Guest
I'm trying to store callbacks that specify parameters in an array and then execute them sequentially but I don't really understand how to achieve this in JS.

What I want to do:

Code:
var callbacks = [
    doThis("foo"),
    doThat("bar")
];

function doThis(a) {
    ...
}

function doThat(b) {
    ...
}

callbacks.forEach(function(callback) {
    callback() // Expression is not callable
});

<p>I'm trying to store callbacks that specify parameters in an array and then execute them sequentially but I don't really understand how to achieve this in JS.</p>
<p>What I want to do:</p>
<pre class="lang-js prettyprint-override"><code>var callbacks = [
doThis("foo"),
doThat("bar")
];

function doThis(a) {
...
}

function doThat(b) {
...
}

callbacks.forEach(function(callback) {
callback() // Expression is not callable
});
</code></pre>
 

Latest posts

Top