Guest User

Untitled

a guest
Jun 8th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. const fs = require('fs') //require the filesystem module: a standard node library
  2. const util = require('util')
  3.  
  4. // const f = fs.readFile(__dirname + '/input.txt', { encoding: 'utf8' })
  5. // console.log('f is', f)
  6.  
  7. // process.exit()
  8.  
  9.  
  10. const USER_FILENAME = __dirname + '/username.txt'
  11.  
  12. /*
  13. //a callback hell
  14.  
  15. fs.readFile(USER_FILENAME, { encoding: 'utf8' },
  16.  
  17. function handleResult(err, username) {
  18. if (err) {
  19. console.log('BUMMER', err.toString())
  20. return
  21. }
  22. console.log('GOT USERNAME FROM FILE:', username)
  23.  
  24. fs.readFile(__dirname + `/password-${username}.txt`, { encoding: 'utf8' },
  25.  
  26. function handleResult(err, data) {
  27. if (err) {
  28. console.log('BUMMER', err.toString())
  29. return
  30. }
  31. console.log('GOT USERNAME FROM FILE:', data)
  32.  
  33. }
  34. )
  35.  
  36. }
  37. )
  38.  
  39. */
  40.  
  41.  
  42. /*
  43. const successHandler = username=> {
  44. console.log(username)
  45. return $.getJSON(`/api/password-${username}`)
  46. }
  47. const errorHandler = console.err.bind(console)
  48.  
  49. const passwordPromise = $.getJSON('/api/username')
  50. passwordPromise
  51. .then(successHandler)
  52. .catch(err=>console.error(err.toString()))
  53.  
  54. //*/
  55.  
  56.  
  57.  
  58. const readFileP = util.promisify(fs.readFile)
  59.  
  60. /*
  61.  
  62. //Promisified standard modules
  63.  
  64. const passwordPromise = readFileP(USER_FILENAME, { encoding: 'utf8' })
  65. .then(username=>readFileP(__dirname + `/password-${username}.txt`, { encoding: 'utf8' }))
  66.  
  67. passwordPromise
  68. .then(data=>console.log('PASSWORD', data))
  69. .catch(err=>console.error(err.toString()))
  70.  
  71. */
  72.  
  73.  
  74. function readUserNameP() {
  75. return readFileP(USER_FILENAME, { encoding: 'utf8' })
  76. }
  77. function readPasswordP(username) {
  78. return readFileP(__dirname + `/password-${username}.txt`, { encoding: 'utf8' })
  79. }
  80.  
  81.  
  82. async function main() {
  83. try {
  84. const username = await readUserNameP()
  85. const password = await readPasswordP(username)
  86. console.log(`USER: ${username}, PASS: ${password}`)
  87. } catch (err) {
  88. console.log('BUMMER', err.toString())
  89. //throw new Error ('DUNNO_WHAT_HAPPENED')
  90. }
  91. }
  92.  
  93. main()
Add Comment
Please, Sign In to add comment