A
A
Axepec2017-03-30 22:52:42
JavaScript
Axepec, 2017-03-30 22:52:42

Asynchronous node, beloved, no result is passed from the function?

Guys, take a look at the code, please, I’ve been fighting for 3 hours, with the node while on you, I can’t cope with this asynchrony, I can’t figure out how to issue an object from the function:

parserGame = (game) ->

  link = game.link
  name = game.name

  resultOneGame =
    name: ''
    days: []

  needle.get link, (err, res) ->
    if !err && res.statusCode == 200

      allDate = []

      $ = cheerio.load(res.body)

      defaultGame = []
      $('.boxesDay').find('.boxes').find('td.box').find('span.time').contents().each ->
        defaultGame.push(this.data.substring(0,5))


      $('.col-header').each ->

        if $(this).parent().hasClass('time') == false

          dayGame =
            date: ''
            game: []

          dayGame.date = $(this).text().slice(0,8)



          defaultGame.forEach (item, i) ->
            arr = [item]
            dayGame.game[i] = arr
            # dayGame.game[i].playd = false

          i = 0


          $(this).next().find('td.box').each ->

            if $(this).hasClass('reserveding')

              obj =
                playd: true

              dayGame.game[i].push(obj)



            $(this).find('span.cell').each ->

              $(this).find('input').each ->

                dayGame.game[i].push(this.attribs.value)
            i++

          # Правильно
          allDate.push(dayGame)

      resultOneGame.name = name
      resultOneGame.days = allDate

      # console.log resultOneGame ----- ВОТ ЗДЕСЬ ВСЕ ЕСТЬ
      return resultOneGame


  return resultOneGame ----- ВОТ ТУТ УЖЕ НЕТ

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton, 2017-03-31
@Axepec

return resultOneGame ----- HERE IS NOT HERE
It will be right here YET! Because at this point, the function passed to needle.get hasn't even started executing yet. There are many solutions.
I'm not friends with CS, so examples in JS:
1) Classic
function parserGame(game, callback) {
..............
if (err || res.statusCode != 200)
  return callback(new Error());
...............
// console.log resultOneGame ----- ВОТ ЗДЕСЬ ВСЕ ЕСТЬ
return callback(null, resultOneGame);
  }
};

function callback(error, resultOneGame) {
 // получаем результат
}

2) Promise
function parserGame(game) {
  return new Promise(reject, resolve) {
  .................................
  if (err || res.statusCode != 200)
    reject(new Error());
  ................................
  // console.log resultOneGame ----- ВОТ ЗДЕСЬ ВСЕ ЕСТЬ
  resolve(resultOneGame);
  }
}

parserGame(game).then(function(resultOneGame) {
// получаем результат
}).catch(function(err){});

M
Mikhail Osher, 2017-03-30
@miraage

Read about context, scopes and asynchronous flow in javascript.
Then you will get an answer to your question.

A
Axepec, 2017-03-31
@Axepec

Anton tell me please... I'm calling a function... everything is in callBacke. Happened. BUT the dog is like this when the function is called, the result is given ->

PassThrough {
  _readableState: 
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: null,
........................................................................

and should return an object
getResultOneGame = (error, resultParse) ->
  console.log resultParse
  return resultParse

the call goes naturally to the main function ..... Ppstst
Although I created test functions, passing the application structure, everything is buzzing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question