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
Author
I figured it out.
Here are some incomplete snippets,
// Fetching data from MongoDB via Mongoose
router.post("/", function(req, res, next) {
async.waterfall(
[
async function(result, cb) {
await query.findOne(function(err, data) {
// action
}
}
]
);
}
// Storing data
try {
let save_data = await new_data.save();
if (save_data) {
// action
}
} catch (err) {
console.log(err);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, we need to export readFile for use in another file. For example, first rename
index.jstofoo.js, then append the following line to the file,Then in
bar.js, include the contents and execute from there,