Suppose you want to make an async request in JavaScript, but you want to pass some state along to the callback method. Is the following an appropriate use of closures in JavaScript?
function getSomethingAsync(someState, callback) {
var req = abc.createRequestObject(someParams);
req.invoke(makeCallback(someState, callback));
}
function makeCallback(someState, callback) {
return function getSomethingCallback(data) {
var result = processDataUsingState(data, someState);
callback(result); // alternately/optionally pass someState along to result
}
}
If not, is there a better or more idiomatic way?