Last active
July 19, 2024 18:11
-
-
Save gicrisf/1c6495d59ac50cddbc75c45a85f7393c to your computer and use it in GitHub Desktop.
Run puppeteer with only promises in a clean way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var puppeteer = require("puppeteer"); | |
| (() => { | |
| let browser; | |
| let page; | |
| puppeteer | |
| // pass `{ headless: false }` if you want to see the browser | |
| .launch() | |
| .then(it => | |
| new Promise | |
| // Move the browser in the let outside | |
| (res => res (browser = it))) | |
| .then(() => | |
| new Promise | |
| // Open a new page | |
| (res => res(browser.newPage()))) | |
| .then(it => | |
| new Promise | |
| // Move the new page outside | |
| (res => res(page = it))) | |
| .then(() => | |
| new Promise | |
| // Open a web page | |
| (res => res (page.goto('https://developer.chrome.com/')))) | |
| .then(it => | |
| new Promise | |
| // Finally, close the browser | |
| (res => res(browser.close()))); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment