0
js and getting into callback I’m having a problem error:
TypeError: callback is not a function
at Timeout._onTimeout (C:\Users\SpiriT\Documents\estudo\callback\index.js:4:16)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
PS C:\Users\SpiriT\Documents\estudo\callback>
my code:
function getUser(callback){
setTimeout( () => {
return callback(null, {
id: 1,
nome: 'gabriel',
dataNascimento: new Date()
})
},1000)
}
function getPhone(idUsuario,callback){ //callback sempre no ultimo param
setTimeout( () => {
return callback(null, {
telefone:'99492566',
dd:73
})
},1000)
}
function getAddress(idUsuario,callback){
setTimeout( () => {
return callback(null,{
rua: 'Fatima',
numero: '197'
})
},1000)
}
getUser(function resolveUser(error,usuario){
if(error){
console.error('fail in user', error);
return;
}
getPhone(usuario.id, function resolvePhone(error1,phone){
if(error1){
console.error('fail in phone', error1);
return;
}
})
getAddress(usuario.id, function resolveAddrsss(error2,address){
if(error2){
console.error('fail in address', error2);
return;
}
})
})
const usuario = getUser();
const phone = getPhone();
const address = getAddress();
You must pass the parameter inside the timeout after the time in milliseconds. Example:
}, 1000, callback)
– Leonardo Getulio
continued with the same error @Leonardogetulio
– Joao Spirit
It turns out that in
getUser()
,getPhone()
andgetAddress()
you’re not passing the callback, therefore the error. To fix, addif (!(callback instanceof Function)) return
before all the Return callback(..)– Valdeir Psr
I found strange I was following a course and I made qse equal just changing name of functions and etc and had given error went to check the video and had done just like .
– Joao Spirit
@Valdeirpsr you could answer how it would look?
– Joao Spirit