Node.js async/await examples
Last active
April 20, 2022 14:01
-
-
Save tatsuyasusukida/5f0850d75be41f922ae6c4e74ee86acc to your computer and use it in GitHub Desktop.
Node.js async/await examples
This file contains 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
# Logs | |
logs | |
*.log | |
npm-debug.log* | |
yarn-debug.log* | |
yarn-error.log* | |
lerna-debug.log* | |
# Diagnostic reports (https://nodejs.org/api/report.html) | |
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | |
# Runtime data | |
pids | |
*.pid | |
*.seed | |
*.pid.lock | |
# Directory for instrumented libs generated by jscoverage/JSCover | |
lib-cov | |
# Coverage directory used by tools like istanbul | |
coverage | |
*.lcov | |
# nyc test coverage | |
.nyc_output | |
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | |
.grunt | |
# Bower dependency directory (https://bower.io/) | |
bower_components | |
# node-waf configuration | |
.lock-wscript | |
# Compiled binary addons (https://nodejs.org/api/addons.html) | |
build/Release | |
# Dependency directories | |
node_modules/ | |
jspm_packages/ | |
# TypeScript v1 declaration files | |
typings/ | |
# TypeScript cache | |
*.tsbuildinfo | |
# Optional npm cache directory | |
.npm | |
# Optional eslint cache | |
.eslintcache | |
# Microbundle cache | |
.rpt2_cache/ | |
.rts2_cache_cjs/ | |
.rts2_cache_es/ | |
.rts2_cache_umd/ | |
# Optional REPL history | |
.node_repl_history | |
# Output of 'npm pack' | |
*.tgz | |
# Yarn Integrity file | |
.yarn-integrity | |
# dotenv environment variables file | |
.env | |
.env.test | |
# parcel-bundler cache (https://parceljs.org/) | |
.cache | |
# Next.js build output | |
.next | |
# Nuxt.js build / generate output | |
.nuxt | |
dist | |
# Gatsby files | |
.cache/ | |
# Comment in the public line in if your project uses Gatsby and *not* Next.js | |
# https://nextjs.org/blog/next-9-1#public-directory-support | |
# public | |
# vuepress build output | |
.vuepress/dist | |
# Serverless directories | |
.serverless/ | |
# FuseBox cache | |
.fusebox/ | |
# DynamoDB Local files | |
.dynamodb/ | |
# TernJS port file | |
.tern-port |
This file contains 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
const fs = require('fs') | |
if (require.main === module) { | |
main() | |
} | |
function main () { | |
try { | |
const buffer = fs.readFileSync('case-01-sync.js') | |
const text = buffer.toString() | |
console.log(text) | |
} catch (err) { | |
console.error(err) | |
} | |
} |
This file contains 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
const fs = require('fs') | |
if (require.main === module) { | |
main() | |
} | |
function main () { | |
const buffer = fs.readFile('case-02-callback.js', (err, buffer) => { | |
if (err) { | |
console.error(err) | |
return | |
} | |
const text = buffer.toString() | |
console.log(text) | |
}) | |
} |
This file contains 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
const fs = require('fs') | |
if (require.main === module) { | |
main() | |
} | |
function main () { | |
const chunks = [] | |
const stream = fs.createReadStream('case-03-stream.js') | |
stream.on('data', chunk => chunks.push(chunk)) | |
stream.on('error', err => console.error(err)) | |
stream.on('end', () => { | |
const buffer = Buffer.concat(chunks) | |
const text = buffer.toString() | |
console.log(text) | |
}) | |
} |
This file contains 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
const fs = require('fs') | |
if (require.main === module) { | |
main() | |
} | |
function main () { | |
new Promise((resolve, reject) => { | |
const buffer = fs.readFile('case-04-promise.js', (err, buffer) => { | |
err ? reject(err) : resolve(buffer) | |
}) | |
}) | |
.then(buffer => { | |
const text = buffer.toString() | |
console.log(text) | |
}) | |
.catch(err => { | |
console.error(err) | |
}) | |
} |
This file contains 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
const fsPromises = require('fs/promises') | |
if (require.main === module) { | |
main() | |
} | |
async function main () { | |
try { | |
const buffer = await fsPromises.readFile('case-05-async.js') | |
const text = buffer.toString() | |
console.log(text) | |
} catch (err) { | |
console.error(err) | |
} | |
} |
This file contains 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
const fs = require('fs') | |
const {Observable} = require('rxjs') | |
if (require.main === module) { | |
main() | |
} | |
function main () { | |
try { | |
const observable = new Observable(subscriber => { | |
const stream = fs.createReadStream('case-06-rxjs.js') | |
const onData = chunk => subscriber.next(chunk) | |
const onError = err => subscriber.error(err) | |
const onEnd = () => subscriber.complete() | |
stream.on('data', onData) | |
stream.on('error', onError) | |
stream.on('end', onEnd) | |
return () => { | |
stream.off('data', onData) | |
stream.off('error', onError) | |
stream.off('end', onEnd) | |
} | |
}) | |
const chunks = [] | |
const subscription = observable.subscribe({ | |
next (chunk) { | |
chunks.push(chunk) | |
}, | |
error (err) { | |
console.error(err) | |
}, | |
complete () { | |
subscription.unsubscribe() | |
const buffer = Buffer.concat(chunks) | |
const text = buffer.toString() | |
console.log(text) | |
}, | |
}) | |
} catch (err) { | |
console.error(err) | |
} | |
} |
This file contains 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
MIT License | |
Copyright (c) 2022 tatsuyasusukida | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
This file contains 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
{ | |
"name": "nodejs-async-await", | |
"version": "1.0.0", | |
"lockfileVersion": 2, | |
"requires": true, | |
"packages": { | |
"": { | |
"name": "nodejs-async-await", | |
"version": "1.0.0", | |
"license": "ISC", | |
"dependencies": { | |
"rxjs": "^7.5.5" | |
} | |
}, | |
"node_modules/rxjs": { | |
"version": "7.5.5", | |
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.5.5.tgz", | |
"integrity": "sha512-sy+H0pQofO95VDmFLzyaw9xNJU4KTRSwQIGM6+iG3SypAtCiLDzpeG8sJrNCWn2Up9km+KhkvTdbkrdy+yzZdw==", | |
"dependencies": { | |
"tslib": "^2.1.0" | |
} | |
}, | |
"node_modules/tslib": { | |
"version": "2.3.1", | |
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", | |
"integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==" | |
} | |
}, | |
"dependencies": { | |
"rxjs": { | |
"version": "7.5.5", | |
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.5.5.tgz", | |
"integrity": "sha512-sy+H0pQofO95VDmFLzyaw9xNJU4KTRSwQIGM6+iG3SypAtCiLDzpeG8sJrNCWn2Up9km+KhkvTdbkrdy+yzZdw==", | |
"requires": { | |
"tslib": "^2.1.0" | |
} | |
}, | |
"tslib": { | |
"version": "2.3.1", | |
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", | |
"integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==" | |
} | |
} | |
} |
This file contains 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
{ | |
"name": "nodejs-async-await", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"rxjs": "^7.5.5" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment