下一页
未定义传递到getPage函数的变量。它只存在于process1函数的范围内。
深入研究
Promises
. 有了它,您可以链接将在彼此之后运行的方法。在success回调中返回一个新的promise,链中的下一个方法将停止,直到当前promise被解析。
function process1($) {
// Process stuff
// What you return here will be passed to the next function in the promise chain below (in this case a string)
return $("a[data-url$='nxtPageId']").attr('data');
}
function process2(nextPage) {
// More processing
// getPage will return a promise which eventually gets resolved with the cheerio object
return getPage(nextPage);
}
function process3($) {
// More processing?
}
getPage('https://friendspage.org')
.then(process1)
.then(process2)
.then(process3)
.catch(err => {
console.log(err);
// error handling here
});