V
V
Vitaly2016-02-19 18:10:41
JavaScript
Vitaly, 2016-02-19 18:10:41

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))

Through asynchrony, when I do console.log(enabled_api(ccc)) gives undefined

Answer the question

In order to leave comments, you need to log in

5 answer(s)
H
HoHsi, 2016-02-21
@Scorpiored88

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()}`);
});

Or still with Async, but you will need 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);
      }
    });
  });
}

(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()}`);
});

D
Dark Hole, 2016-02-19
@abyrkov

You don't return it. lol

A
Alexander Wolf, 2016-02-19
@mannaro

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

V
Vladimir Boliev, 2016-02-20
@voff

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.

L
lem_prod, 2016-02-20
@lem_prod

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 question

Ask a Question

731 491 924 answers to any question