I'm making a small reusable module for my use. and stopped on a point in my code which is, how can I make sure that the consumer of my module has called a function I provided to him or not in one of the methods of my module?
Basically, the API of the module looks like the forEach method, where my module passes internally arguments just like how forEach works
myModule.myMethod(callbackFunc, z)
myModule
internally calls your callbackFunc
with the following arguments: anotherFunc
, x
, y
myModule.myMethod((anotherFunc, x, y) => {
if (x == true) anotherFunc()
}, z)
How can I know that the consumer of the myMethod
has called passed_function
or not?
Assuming anotherFunc
is something you create internally, you can write it so that it updates a boolean, and then later you can check that boolean. For example:
function myMethod(callback, z) {
let hasBeenCalled = false;
const anotherFunc = () => {
hasBeenCalled = true;
// ...
}
callback(anotherFunc, 'hello', 'world');
if (hasBeenCalled) {
console.log('they called it!');
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.