Skip to content

Instantly share code, notes, and snippets.

View sequentially.js
module.exports = function sequentially(fns) {
var results = [], last;
for (var k = 0; k < fns.length; ++k)
if (k === 0) results.push(last = fns[k]());
else results.push(last = last.then(fns[k]))
return results;
}
// Usage
View removed-resolve.md

[Decision to remove resolve][1]

Not sure if this is correct, but: This might actually be (perhaps accidentally) one step closer to allowing promises for thenables in a cleaner way if the need arises in the future.

Since its impossible to create a promise for a promise now, it would be theoretically possible to just

  • introduce Promise.of (wraps thenables)
  • make .then unwrap "true" promises only once
  • make resolve cast thenables into promises that recursively unwrap thenables
View index.js
var fs = require('fs');
var path = require('path');
// Load the typescript module
var TypeScript = (function(){
var sandbox = {
__filename: __filename,
__dirname: __dirname,
global: global,
process: process,
View pipe-promises.js
exports.pipe = function (source, sink) {
var resolve, reject;
return new Promise(function(resolve_, reject_) {
resolve = resolve_;
reject = reject_;
source
.on("error", reject)
.pipe(sink)
.on("error", reject);
.on("finish", resolve);
View myequal.hs
myequal :: Eq a => [a] -> [a] -> Bool
-- two empty lists are equal
myequal [] [] = True
-- two non-empty lists are equal when their first elements are equal and the remainder of the lists are also equal
myequal (x:xs) (y:ys) = x == y && myeqal xs ys
-- all other lists are not equal
myequal _ _ = False
View recursive-acronym-charat.js
function solve(acronym, expansion, n) {
var pos = expansion.indexOf(acronym),
translated = pos ? n % pos :
acronym.length +
(n - acronym.length) %
(expansion.length - acronym.length);
return expansion[translated];
}
View extends.js
// JavaScript inheritance with comments
function extends(Derived, Base) {
// First, we copy all static members from base to derived.
for (var staticMember in Base)
if (Base.hasOwnProperty(staticMember))
Derived[staticMember] = Base[staticMember];
// Then, we create a tiny helper "class" that constructs derived's prototype,
// which just adds the constructor property to its instance.
View maplimit.js
var Promise = require('bluebird');
function mapLimit(arr, n, f) {
var queued = [];
var results = arr.map(function(item) {
var mustComplete = Math.max(0, queued.length - n + 1);
var thisone = Promise.some(queued, mustComplete)
.thenReturn(item).then(f);
queued.push(thisone);
View pager-promises.js
function Pager(url) {
this._url = url;
this._prev = null; // Pager
this._data = null; // Promise<Data>, where Data { endKey, rows }
this._startKey = Promise.resolve(null);
}
Pager.prototype.data = function() {
if (!this._data)
View bluebird-tests.ts
/// <reference path="bluebird.d.ts" />
import Promise = require("bluebird");
Promise.cast(8).then(x => console.log(x.toExponential()));
function delayer(time: number) {
var d = Promise.defer();
setTimeout(_ => d.resolve, time);
You can’t perform that action at this time.