-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support modules * update code
- Loading branch information
Showing
6 changed files
with
1,453 additions
and
11 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// top-level async/await asynchronous example | ||
|
||
const fs = require('fs').promises; | ||
|
||
const filePath = './file.txt'; | ||
|
||
// `async` before the parent function | ||
try { | ||
// `await` before the async method | ||
const data = await fs.readFile(filePath, 'utf-8'); | ||
console.log(data); | ||
console.log('Done!'); | ||
} catch (error) { | ||
console.log('An error occurred...: ', error); | ||
} | ||
console.log("I'm the last line of the file!"); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Create a basic promise function | ||
function promiseFunction() { | ||
return new Promise((resolve, reject) => { | ||
|
||
// do something | ||
|
||
if(error){ | ||
// indicate success | ||
reject(error) | ||
} else { | ||
// indicate error | ||
resolve(data) | ||
} | ||
}) | ||
} | ||
|
||
// Call a basic promise function | ||
promiseFunction() | ||
.then((data) => { | ||
// handle success | ||
}) | ||
.catch((error) => { | ||
// handle error | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
// promises asynchronous example | ||
|
||
const fs = require('fs').promises; | ||
|
||
const filePath = './file.txt'; | ||
|
||
// request to read a file | ||
fs.readFile(filePath, 'utf-8') | ||
.then((data) => { | ||
console.log(data); | ||
console.log('Done!'); | ||
}) | ||
.catch((error) => { | ||
console.log('An error occurred...: ', error); | ||
}); | ||
.then((data) => { | ||
console.log(data); | ||
console.log('Done!'); | ||
}) | ||
.catch((error) => { | ||
console.log('An error occurred...: ', error); | ||
}); | ||
|
||
console.log(`I'm the last line of the file!`) |
Oops, something went wrong.