@@ -18,42 +18,48 @@ const run = async () => {
1818 "First page links" ,
1919 extractLinks ( page1 . organic_results ) ,
2020 ) ;
21- let page2 = await page1 . next ?. ( ) ;
22- console . log (
23- "Second page links" ,
24- extractLinks ( page2 ?. organic_results ) ,
25- ) ;
21+ if ( page1 . next ) {
22+ let page2 = await page1 . next ( ) ;
23+ console . log (
24+ "Second page links" ,
25+ extractLinks ( page2 . organic_results ) ,
26+ ) ;
27+ }
2628
2729 // Pagination (callback)
2830 getJson ( "google" , params , ( page1 ) => {
2931 console . log (
3032 "First page links" ,
3133 extractLinks ( page1 . organic_results ) ,
3234 ) ;
33- page1 . next ?. ( ( page2 ) => {
34- console . log (
35- "Second page links" ,
36- extractLinks ( page2 . organic_results ) ,
37- ) ;
38- } ) ;
35+ if ( page1 . next ) {
36+ page1 . next ( ( page2 ) => {
37+ console . log (
38+ "Second page links" ,
39+ extractLinks ( page2 . organic_results ) ,
40+ ) ;
41+ } ) ;
42+ }
3943 } ) ;
4044
4145 // Use global config
4246 config . api_key = apiKey ;
4347 page1 = await getJson ( "google" , { q : "Coffee" } ) ;
44- page2 = await page1 . next ?. ( ) ;
45- console . log (
46- "Second page links" ,
47- extractLinks ( page2 ?. organic_results ) ,
48- ) ;
48+ if ( page1 . next ) {
49+ page2 = await page1 . next ( ) ;
50+ console . log (
51+ "Second page links" ,
52+ extractLinks ( page2 . organic_results ) ,
53+ ) ;
54+ }
4955
5056 // Pagination loop (async/await)
5157 let links = [ ] ;
5258 let page = await getJson ( "google" , { q : "Coffee" } ) ;
5359 while ( page ) {
5460 links . push ( ...extractLinks ( page . organic_results ) ) ;
5561 if ( links . length >= 30 ) break ;
56- page = await page . next ?. ( ) ;
62+ page = page . next ? await page . next ( ) : undefined ;
5763 }
5864 console . log ( links ) ;
5965
0 commit comments