Пытаюсь познать промисы, но никак не получается. В чем проблема?
let getMoviePage = new Promise(function(resolve, reject) {
Request.get(searchParams, (err, res, page) => {
console.log(`movie: ${searchParams.uri}`);
const $ = cheerio.load(page);
resolve($('iframe').attr('src'));
});
});
app.get('/', (request, response) => {
getMoviePage
.then(iframe => {
Request.get(iframe, (err, res, page) => {
console.log(`iframe: ${playlistFrameLink}`);
const $ = cheerio.load(page);
let filePath = $.html();
let playlistLink = Cut(filePath, { fragmentStart: 'var flashvars = ', fragmentEnd: '\'};' });
// delete spaces
playlistLink = playlistLink.replace(/\s+/g, ' ');
//...
return playlistLink;
});
})
.then(playlist => {
console.log(`playlist: ${playlist}`);
Request.get(playlist, (err, res, page) => {
const $ = cheerio.load(page);
return JSON.parse($('body').text());
});
})
.then(playlist => {
console.log(playlist);
//...
response.send(playlistWeight);
})
.catch(err => {
console.error(err);
});
});