@@ -62,12 +62,13 @@ class Scraper {
6262 this . options = extend ( defaults , options ) ;
6363 this . options . request = extend ( defaults . request , options . request ) ;
6464
65- this . options . urls = Array . isArray ( this . options . urls ) ? this . options . urls : [ this . options . urls ] ;
66- this . options . urls . forEach ( ( urlItem , i ) => {
65+ const urls = Array . isArray ( options . urls ) ? options . urls : [ options . urls ] ;
66+
67+ this . options . urls = urls . map ( ( urlItem ) => {
6768 if ( typeof urlItem === 'string' ) {
68- this . options . urls [ i ] = { url : urlItem , filename : this . options . defaultFilename } ;
69+ return { url : urlItem , filename : this . options . defaultFilename } ;
6970 } else {
70- this . options . urls [ i ] = { url : urlItem . url , filename : urlItem . filename || this . options . defaultFilename } ;
71+ return { url : urlItem . url , filename : urlItem . filename || this . options . defaultFilename } ;
7172 }
7273 } ) ;
7374
@@ -125,21 +126,19 @@ class Scraper {
125126 }
126127 }
127128
128- saveResource ( resource ) {
129+ async saveResource ( resource ) {
129130 resource . setSaved ( ) ;
130131
131- return Promise . resolve ( )
132- . then ( ( ) => this . resourceHandler . handleResource ( resource ) )
133- . then ( ( ) => {
134- logger . info ( 'saving resource ' + resource + ' to fs' ) ;
135- return this . runActions ( 'saveResource' , { resource} ) ;
136- } ) . then ( ( ) => {
137- // ignore promise here, just notifying external code about resource saved
138- this . runActions ( 'onResourceSaved' , { resource} ) ;
139- } ) . catch ( ( err ) => {
140- logger . warn ( 'failed to save resource ' + resource ) ;
141- return this . handleError ( err , resource ) ;
142- } ) ;
132+ try {
133+ await this . resourceHandler . handleResource ( resource ) ;
134+ logger . info ( 'saving resource ' + resource + ' to fs' ) ;
135+ await this . runActions ( 'saveResource' , { resource} ) ;
136+ // ignore promise here, just notifying external code about resource saved
137+ this . runActions ( 'onResourceSaved' , { resource} ) ;
138+ } catch ( err ) {
139+ logger . warn ( 'failed to save resource ' + resource ) ;
140+ return this . handleError ( err , resource ) ;
141+ }
143142 }
144143
145144 createNewRequest ( resource ) {
@@ -199,17 +198,17 @@ class Scraper {
199198 return requestPromise ;
200199 }
201200
202- requestResource ( resource ) {
201+ async requestResource ( resource ) {
203202 const url = resource . getUrl ( ) ;
204203
205204 if ( this . options . urlFilter && ! this . options . urlFilter ( url ) ) {
206205 logger . debug ( 'filtering out ' + resource + ' by url filter' ) ;
207- return Promise . resolve ( null ) ;
206+ return null ;
208207 }
209208
210209 if ( this . options . maxDepth && resource . getDepth ( ) > this . options . maxDepth ) {
211210 logger . debug ( 'filtering out ' + resource + ' by depth' ) ;
212- return Promise . resolve ( null ) ;
211+ return null ;
213212 }
214213
215214 if ( this . requestedResourcePromises . has ( url ) ) {
@@ -254,15 +253,15 @@ class Scraper {
254253 return Promise . resolve ( this . resources ) ;
255254 }
256255
257- handleError ( err , resource ) {
256+ async handleError ( err , resource ) {
258257 // ignore promise here, just notifying external code about resource error
259258 this . runActions ( 'onResourceError' , { resource, error : err } ) ;
260259
261260 if ( this . options . ignoreErrors ) {
262261 logger . warn ( 'ignoring error: ' + err . message ) ;
263- return Promise . resolve ( null ) ;
262+ return null ;
264263 }
265- return Promise . reject ( err ) ;
264+ throw err ;
266265 }
267266
268267 async errorCleanup ( error ) {
0 commit comments