Q
Q
qwerty2018-01-31 20:15:49
JavaScript
qwerty, 2018-01-31 20:15:49

How can I return the result of a callback function in a function?

What needs to be done so that the do function returns the result of the getUrl function?

function do () {
    getUrl(function () {
        let a = 5;
        return a;
    }
}

console.log(do()); //5

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail, 2018-01-31
@Bapen1k

Learn Promises

function do() {
  return new Promise((res, rej) => {
      getUrl(() => {
          const a = 5;
          res(a);
      })
  })
}

do().then(res => console.log(res));

A
Abcdefgk, 2018-02-01
@Abcdefgk

Neither callback functions, nor promises, nor "promised functions", nor async functions can return values. Sic!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question