-2

I need to get the first response of "second" function, then the "first" function.

function first(n,callback) {
    setTimeout(function() {
        console.log(n);
    }, 1000);
    callback();
}

function callback() {
    console.log('2');
}

first(5,callback);
Ankush Sharma
  • 677
  • 3
  • 11
kompaniietst
  • 250
  • 3
  • 10
  • 1
    hi! Judging by your name (so sorry if I'm wrong) you might find it easier to ask questions a [Stack Overflow на русском](https://ru.stackoverflow.com) – Matt Ellen Oct 24 '18 at 09:45
  • Put the `callback()` call inside the function that you pass to `setTimeout` – Bergi Oct 24 '18 at 09:50

1 Answers1

0

You can use Promise.resolve and execute setTimeout inside the resolve & inside then callback execute the callback

function first(n, callback) {
  return new Promise(resolve => setTimeout(() => resolve(console.log(n)), 1000)).then(() => {
    callback()
  })

}

function callback() {
  console.log('2');
}

first(5, callback);
brk
  • 48,835
  • 10
  • 56
  • 78