amark/gun · error · TypeError
Callback is not a function
Error message
Callback is not a function
What it means
yson.stringifyAsync serializes data asynchronously and delivers the result via a callback. It validates that the second argument is actually a function and throws a TypeError when it is not — catching cases where a non-callable (string, object, undefined) was passed in the callback position.
Source
Thrown at lib/ison.js:687
throw new Error('Missing Callback');
intensity = validateIntensity(intensity);
return parseWrapper(data, reviver, intensity, callback);
};
/**
* Error checking and call of appropriate functions for JSON stringify API
* @param { primitive data types } data
* @param { function or array } replacer
* @param { number or string } space
* @param { number } intensity
* @param { function } callback
* @return { function } stringifyWrapper
*/
yson.stringifyAsync = function(data, callback, replacer = null, space, intensity = 1) {
if (typeof callback !== 'function') {
throw new TypeError('Callback is not a function');
}
if (typeof space === 'number' || typeof space === 'string')
space = validateSpace(space);
if (typeof intensity === 'number')
intensity = validateIntensity(intensity);
return stringifyWrapper(data, replacer, space, intensity, callback);
}
if(typeof window != ''+u){ window.YSON = yson }
try{ if(typeof module != ''+u){ module.exports = yson } }catch(e){}
if(typeof JSON != ''+u){
JSON.parseAsync = yson.parseAsync;
JSON.stringifyAsync = yson.stringifyAsync;
}
}());
View on GitHub (pinned to 552227599d)
Solutions
- Pass a function as the second argument: yson.stringifyAsync(data, (err, res) => { ... })
- Verify argument order: callback is argument 2, replacer argument 3, space argument 4, intensity argument 5
- Ensure the callback variable is defined and is a function at the call site
- Wrap in a type check in dev: if (typeof cb !== 'function') throw before calling
Example fix
// before — old JSON.stringify order leaks replacer into callback slot
yson.stringifyAsync(data, replacer, 2, (err, res) => {});
// after
yson.stringifyAsync(data, (err, res) => {
if (err) return console.error(err);
}, replacer, 2); Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof callback !== 'function') {
throw new TypeError('stringifyAsync requires a function callback');
} Type guard
const isFn = (v) => typeof v === 'function';
if (!isFn(cb)) throw new TypeError('callback must be a function'); Try / catch
try {
yson.stringifyAsync(data, (err, res) => { if (err) throw err; }, replacer, space, intensity);
} catch (e) {
console.error('stringifyAsync setup failed:', e.message);
} Prevention
- Keep JSON.stringify argument-order in mind but adapt: callback is 2nd, replacer 3rd
- Type-check the callback variable before calling
- Centralize serialization in a helper that injects the callback
When it happens
Trigger: Calling yson.stringifyAsync(data) with no callback; passing a non-function (e.g. a replacer string or space value) in the second position due to argument-order confusion with the signature (data, callback, replacer, space, intensity); a variable holding the callback being undefined at call time.
Common situations: Refactoring from synchronous JSON.stringify(value, replacer, space) and keeping the old argument order (replacer second) — here replacer must be third; copying stringify code that omitted the callback; typos in the callback variable name.
Related errors
- Missing Callback
- First argument must be a string, Buffer, ArrayBuffer, Array,
- Invalid first argument for type 'hex'.
- First argument must be Array containing ArrayBuffer or Uint8
AI-assisted analysis of amark/gun@552227599d (2026-09-02).
Data as JSON: /api/errors/bcc8c0f2ed000090.
Report an issue: GitHub.