Skip to content

Commit 6c31bf5

Browse files
raurirs0ph1e
authored andcommitted
Add example with random delays for beforeRequest action to readme (#368)
* update readme * to tabs
1 parent 0d45ac0 commit 6c31bf5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,19 @@ Should return object which includes custom options for [request](https://github.
270270
If multiple actions `beforeRequest` added - scraper will use `requestOptions` from last one.
271271
```javascript
272272
// Add ?myParam=123 to querystring for resource with url 'http://example.com'
273-
registerAction('beforeRequest', ({resource, requestOptions}) => {
273+
registerAction('beforeRequest', async ({resource, requestOptions}) => {
274274
if (resource.getUrl() === 'http://example.com') {
275275
return {requestOptions: extend(requestOptions, {qs: {myParam: 123}})};
276276
}
277277
return {requestOptions};
278278
});
279+
280+
// Server rejecting a scrape attempt, simply add in some synthetic delays
281+
registerAction('beforeRequest', async ({ resource, requestOptions }) => {
282+
const time = Math.round(Math.random() * 10000);
283+
await new Promise((resolve) => setTimeout(resolve, time));
284+
return { requestOptions };
285+
});
279286
```
280287

281288
##### afterResponse

0 commit comments

Comments
 (0)