View blog-async-await-waterfall.js
async function addComment(comment) {
const post = await fetchPostBySlug(comment.postSlug);
if (!post.isCommentingEnabled) {
throw new Error('Commenting is disabled.');
}
const isAllowed = await isUserAllowedToComment(post, comment.user);
if (!isAllowed) {
View blog-async-await-chained-promises.js
function addComment(comment) {
return fetchPostBySlug(comment.postSlug)
.then((post) => {
if (!post.isCommentingEnabled) {
return Promise.reject(new Error('Commenting is disabled.'));
}
return isUserAllowedToComment(post, comment.user)
.then((isAllowed) => ({ isAllowed, post }));
}).then((input) => {
View blog-async-await-nested-promises.js
function addComment(comment) {
return fetchPostBySlug(comment.postSlug).then((post) => {
if (!post.isCommentingEnabled) {
return Promise.reject(new Error('Commenting is disabled.'));
}
return isUserAllowedToComment(post, comment.user).then((isAllowed) => {
if (!isAllowed) {
return Promise.reject(
new Error('The user is not allowed to comment on this post.')
View blog-async-await-callbacks.js
function addComment(comment, done) {
fetchPostBySlug(comment.postSlug, (err, post) => {
if (err) return done(err);
if (!post.isCommentingEnabled) {
return done(new Error('Commenting is disabled.'));
}
isUserAllowedToComment(post, comment.user, (err, isAllowed) => {
if (err) return done(err);
View async-await-1.json
{
"user": "58bb380d222c2239a2b16ae9",
"postSlug": "is-learning-dangerous",
"comment": "A little learning is a dangerous thing."
}
View reader-t.js
const {tagged} = require('daggy');
// const
const K = (a) => (b) => a;
// Reader Transformer
module.exports = (M) => {
const ReaderT = tagged('run');
ReaderT.lift = ReaderT.prototype.lift = (m) => ReaderT(K(m));
View reader-t.js
const {tagged} = require('daggy');
// const
const K = (a) => (b) => a;
// Reader Transformer
module.exports = (M) => {
const ReaderT = tagged('run');
ReaderT.lift = (m) => ReaderT(K(m));
View reader_future.js
/**
* This short program will encrypt the user password
* and insert a new record into a mock database.
*/
const Reader = require('fantasy-readers');
const Future = require('fluture');
const R = require('ramda');
const crypto = require('crypto');
// our mock database
View reader.js
/**
* This short program will encrypt the user password
* and insert a new record into a mock database.
*/
const Reader = require('fantasy-readers');
const R = require('ramda');
const crypto = require('crypto');
// our mock database
const database = [
View continuation.js
const {tagged} = require('daggy');
let Continuation = tagged('x');
Continuation.prototype.of = Continuation.of = x => {
return new Continuation((resolve) => resolve(x));
}
Continuation.prototype.chain = function(f) {
return this.x(f);