Answer the question
In order to leave comments, you need to log in
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
return resultOneGame ----- HERE IS NOT HEREIt 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.
function parserGame(game, callback) {
..............
if (err || res.statusCode != 200)
return callback(new Error());
...............
// console.log resultOneGame ----- ВОТ ЗДЕСЬ ВСЕ ЕСТЬ
return callback(null, resultOneGame);
}
};
function callback(error, resultOneGame) {
// получаем результат
}
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){});
Read about context, scopes and asynchronous flow in javascript.
Then you will get an answer to your question.
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,
........................................................................
getResultOneGame = (error, resultParse) ->
console.log resultParse
return resultParse
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question