简体   繁体   中英

Can I pass a parameter in the fetch callback?

I need to pass an external parameter to fecth's callback. I know that the callback is asynchronous but I need to have one or more external parameters inside it.

for(var m=0; m<10; m++){          
  fetch("https://www.apiexample.com?key=12345", {    
        method: 'GET'
    })
    .then(response => response.json())
    .then(result => {
        callbackFunction(result);        
    })
    .catch(error => alert('Ops! '+ error));
}

function callbackFunction(result){    
    console.log(result);      //ok
    //... HERE I WANT m VALUE
    
}

Thanks

for(var m=0; m<10; m++){
fetch("https://www.apiexample.com?key=12345", {
method: 'GET' })
.then(response => response.json())
.then(result => {
callbackFunction(result, value );
})
.catch(error => alert('Ops! '+ error));
}
function callbackFunction(result, value )
{
console.log(result);
console.log(value);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM