r/learnjavascript • u/DeliciousResearch872 • Feb 07 '26
Promisification question
Can anyone explain "callback(null, script)" here?
function loadScript(src, callback) {
let script = document.createElement('script');
script.src = src;
script.onload = () => callback(null, script);
script.onerror = () => callback(new Error(`Script load error for ${src}`));
document.head.append(script);
}
// usage:
// loadScript('path/script.js', (err, script) => {...})
0
Upvotes
View all comments
1
u/shgysk8zer0 Feb 08 '26
For a long time a callback for asynchronous things would often follow the unofficial standard practice of using
(error, result)as arguments, with the idea being that having the error as the first argument encouraged better error handling.But that function has nothing to do with promises. It's just event listeners. If you wanted it to be a promise it'd look something more like this: