Skip to content

Instantly share code, notes, and snippets.

Andrew Junior Ongi Karyanto pionize

Block or report user

Report or block pionize

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View .js
async function printFiles () {
const files = await getFilePaths();
await Promise.all(files.map(async (file) => {
const contents = await fs.readFile(file, 'utf8')
console.log(contents)
}));
}
View .js
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)
})
}
View .js
//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
@pionize
pionize / .js
Created Jul 25, 2017
conditional if, promise vs async/await
View .js
//implements Promise way
const getUserPromise = (name) => {
return getUserByName('andrew')
.then(function (user) {
if (user.role == 'guest') {
return user;
} else {
return false;
}
})
@pionize
pionize / .js
Created Jul 25, 2017
Async/Await
View .js
const getUser = async (name) => {
const user = await getUserByName(name);
const account = await getUserAccountById(user.id);
return account;
}
const myUser = getUser('andrew');
View .js
Promise.resolve(someSynchronousValue).then(/* ... */);
View .js
new Promise(function (resolve, reject) {
resolve(someSynchronousValue);
}).then(/* ... */);
View promiseReturn.js
getUserByName('andrew').then(function (user) {
return getUserAccountById(user.id);
}).then(function (userAccount) {
// userAccount data here
});
@pionize
pionize / rookieMistake5.js
Created Apr 17, 2017
Promise without return
View rookieMistake5.js
somePromise().then(function () {
someOtherPromise();
}).then(function () {/* ... */});
View bluebirdPromise.js
// 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);
});
You can’t perform that action at this time.