我想在function。内部调用回调函数,所以我不知道该怎么做
function call(){
pg.connect(conString, function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
client.query('INSERT into post1 (data) VALUES ($n)', function(err, result) {
//call `done()` to release the client back to the pool
done();
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].number);
//output: 1
});
});
}
board.on("ready", function() {
// Create a new generic sensor instance for
// a sensor connected to an analog (ADC) pin
var sensor = new five.Sensor("A0");
// When the sensor value changes, log the value
sensor.on("change", function() {
var n = this.value();
//i want to call that function here
});
});
我也想在另一个回调函数中调用此函数,这是做或建议我正确的方法的正确方法。
您可以执行类似的操作,将函数传递到函数中。因此,在这种情况下,回调将是一个函数。
function call(callback){
pg.connect(conString, function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
//call `done()` to release the client back to the pool
done();
callback(); //execute here or wherever
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].number);
//output: 1
});
});
}
那么你可以这样称呼它
call(function(){
//some logic here.
})
或者:
var someFunction = function()
{
//do something
}
call(someFunction);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句