Skip to content

Instantly share code, notes, and snippets.

Tim Navrotskyy dypsilon

Block or report user

Report or block dypsilon

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 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."
}
@dypsilon
dypsilon / reader-t.js
Last active Dec 27, 2017
In this example, the save method produces an error and we use mapLower to call a special Future function "mapRej", which transforms all incoming errors.
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));
@dypsilon
dypsilon / reader-t.js
Last active Sep 24, 2018
A simple example of using Reader transformer to combine reader and future.
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));
@dypsilon
dypsilon / reader_future.js
Created Aug 9, 2016
Reader + Future Monads Usage
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
@dypsilon
dypsilon / reader.js
Last active Aug 3, 2019
Example usage of the reader monad.
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 = [
@dypsilon
dypsilon / continuation.js
Created Aug 8, 2016
Eager Continuation in JavaScript
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);
You can’t perform that action at this time.