Answer the question
In order to leave comments, you need to log in
How to return a result from an asynchronous function?
Kam me from this code to return ress
function enabled_api (conf){
var ress ;
var config = {
host: conf.host,
username: conf.us,
password: conf.pa
};
exec(config, '/ip service set api disabled=no', function (error, response) {
ress = error ? 'error' : 'ok';
console.log('ress is ', ress)
return ress
})
console.log('ress again is ', ress)
}
var ccc = {host:'1.1.1.1',us:'user,pa:'123'}
console.log(enabled_api(ccc))
Answer the question
In order to leave comments, you need to log in
Firstly, you call the asynchronous version of exec , there is its synchronous counterpart: execsync
Secondly, it will not do without promises / collections / Async / Await.
In the current build, I would recommend using promises, but I myself have already switched to Async / Await with babel.
function enabledApi(conf) {
const config = {
host: conf.host,
username: conf.us,
password: conf.pa
};
return new Promise((resolve, reject) => {
exec(config, '/ip service set api disabled=no', (error, response) => {
const res = error ? 'error' : 'ok';
console.log(`res is ${res}`);
if (error) {
reject(error);
} else {
resolve(response);
}
});
});
}
const config = {
host:'1.1.1.1',
us:'user',
pa:'123'
};
enabledApi(config)
.then((res) => {
console.log(`ress again is ${res}`);
})
.catch((err) => {
console.error(`OMG! ${err.toString()}`);
});
function enabledApi(conf) {
const config = {
host: conf.host,
username: conf.us,
password: conf.pa
};
return new Promise((resolve, reject) => {
exec(config, '/ip service set api disabled=no', (error, response) => {
const res = error ? 'error' : 'ok';
console.log(`res is ${res}`);
if (error) {
reject(error);
} else {
resolve(response);
}
});
});
}
(async () => {
const config = {
host:'1.1.1.1',
us:'user',
pa:'123'
};
const res = await enabledApi(config);
console.log(res);
})()
.catch((err) => {
console.error(`OMG! ${err.toString()}`);
});
For, callback functions are used, which are (usually) passed as the last argument. Also, a promise has long been invented, which allows us to simplify this task.
jsbin.com/zifuyotina/edit?js,console
The whole point of async is that you don't return a result to the main script, you don't wait for that result . Anything you want to do with the result must be done within the callback function.
I won’t write specifically about the script, because I didn’t quite understand what was happening, but I’ll try to explain about asynchrony, when an asynchronous function is launched, arguments are passed to it (their number varies, depending on the function) and the last argument is the callback function , in translation, a callback, this function has access to the results of the previous one, and it can do what it needs with this result, or return the result to the general stream, where it will be processed, pass it to another function, send the result to the browser, etc. . ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question