Created
February 26, 2020 23:41
-
-
Save oxr463/4b8768a4e927b6d03e00e408b3da9bf5 to your computer and use it in GitHub Desktop.
Escape async mode in JavaScript
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
| #!/usr/bin/env node | |
| # SPDX-License-Identifier: MIT | |
| "use strict"; | |
| const fs = require("fs"); | |
| function main() { | |
| let path = "test.txt"; | |
| (async () => { | |
| let x = await handlePromise(path); | |
| console.log(x); | |
| })(); | |
| return 0; | |
| } | |
| async function readFile(path) { | |
| return new Promise((resolve, reject) => { | |
| fs.readFile(path, "utf-8", (err, data) => { | |
| if (err) { | |
| console.log("[*]\tFile Bad."); | |
| reject(err); | |
| } else | |
| resolve(data); | |
| }); | |
| }); | |
| } | |
| async function handlePromise(path) { | |
| let x = await readFile(path); | |
| return x; | |
| } | |
| main(); |
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
| Hello World |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I figured it out.
Here are some incomplete snippets,