JSFiddle - React, Tailwind, and code Playground

by Danny Michaelis

JavaScript

function last (array) {
	return array[array.length - 1]
} 
class ChainFreak {
	constructor () {
  	this.val = 0
    this.ret = []
  }
	addValue () {
  	const value = (last(this.ret) || this.val) + 1;
    this.ret.push(value)
    return this;    
  }  
  printValue () {
    const value = (last(this.ret) || this.val);
    console.log(value);
    return this;
  }
  setValue(v) {
		const value = (last(this.ret) || v);
		this.val = value
		return this
  }
  clean() {
    const value = last(this.ret);
		this.ret = [];
		return value;
  }
}

const cf = new ChainFreak()
console.log(cf.addValue().addValue().addValue())
console.log(cf.setValue().clean())
console.log(cf)

// wrap around promise