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
async function printFiles () { | |
const files = await getFilePaths(); | |
await Promise.all(files.map(async (file) => { | |
const contents = await fs.readFile(file, 'utf8') | |
console.log(contents) | |
})); | |
} |
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
import fs from 'fs-promise' | |
async function printFiles () { | |
const files = await getFilePaths() // Assume this works fine | |
files.forEach(async (file) => { | |
const contents = await fs.readFile(file, 'utf8') | |
console.log(contents) | |
}) | |
} |
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
//implements Promise | |
const getAllUsers = () => { | |
try { | |
getUsers() | |
.then(result => { | |
// this parse may fail | |
const data = JSON.parse(result) | |
console.log(data) | |
}) | |
// uncomment this block to handle asynchronous errors |
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
//implements Promise way | |
const getUserPromise = (name) => { | |
return getUserByName('andrew') | |
.then(function (user) { | |
if (user.role == 'guest') { | |
return user; | |
} else { | |
return false; | |
} | |
}) |
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 getUser = async (name) => { | |
const user = await getUserByName(name); | |
const account = await getUserAccountById(user.id); | |
return account; | |
} | |
const myUser = getUser('andrew'); |
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
Promise.resolve(someSynchronousValue).then(/* ... */); |
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
new Promise(function (resolve, reject) { | |
resolve(someSynchronousValue); | |
}).then(/* ... */); |
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
getUserByName('andrew').then(function (user) { | |
return getUserAccountById(user.id); | |
}).then(function (userAccount) { | |
// userAccount data here | |
}); |
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
somePromise().then(function () { | |
someOtherPromise(); | |
}).then(function () {/* ... */}); |
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
// using Bluebird Promise | |
const Promise = require('bluebird'); | |
new Promise(function (resolve, reject) { | |
fs.readFile('myfile.txt', function (err, file) { | |
if (err) { | |
return reject(err); | |
} | |
resolve(file); | |
}); |
NewerOlder