F
F
freshik3122019-03-29 19:21:26
JavaScript
freshik312, 2019-03-29 19:21:26

How to deal with javascript (node ​​js) asynchrony?

Good afternoon! I study node, I try to overcome asynchrony (without async library)

console.log('start')

let userSearch = (cb)=>{
  connection.query("SELECT * FROM `users` WHERE `login` = ?", [data.login], (err, res)=>{
    if (err) throw err;
    console.log(`result:`);
    console.log(fld);
    cb(fld);
  })
}

console.log(userSearch());

console.log('end')

why when trying to work on callbacks, the output is:
-start
-end
-мои данные

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-03-29
@freshik312

Don't fight it, you won't win.
It needs to be understood and used. To understand - read about EventLoop.
To use - read about Promise, and async / await (in recent versions, or with a transpiler)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question