Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promiz

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promiz - npm Package Compare versions

Comparing version 0.0.1 to 0.1.1

promiz.min.js

2

package.json
{
"name": "promiz",
"version": "0.0.1",
"version": "0.1.1",
"description": "A proper compact promise (promises/A+ spec compliant) library.",

@@ -5,0 +5,0 @@ "main": "promiz.js",

@@ -1,181 +0,229 @@

var promiz = (function(){
function defer(){
(function(){
this.state = 'pending'
this.value
this.stack = []
this.throwing = false
function nextTick(fn){
if(typeof process !== 'undefined' && process.nextTick) {
process.nextTick(fn)
} else {
setTimeout(fn, 0)
}
}
this.resolve = function(val){
if (this.state === 'pending'){
this.state = 'resolved'
this.fire(val)
var promiz = (function(){
function defer(){
this.state = 'pending'
this.value
this.stack = []
this.throwing = false
this.resolve = function(val){
if (this.state === 'pending'){
this.state = 'resolved'
this.fire(val)
}
return this
}
return this
}
this.reject = function(val){
if (this.state === 'pending'){
this.state = 'rejected'
this.fire(val)
this.reject = function(val){
if (this.state === 'pending'){
this.state = 'rejected'
this.fire(val)
}
return this
}
return this
}
this.then = function(fn, er){
this.stack.push([fn || function(){}, er])
if (this.state !== 'pending') {
this.then = function(fn, er){
this.stack.push([fn || function(){}, er])
if (this.state !== 'pending') {
this.fire()
}
return this
}
return this
}
this.done = function(){
this.throwing = true
if (this.state !== 'pending') {
this.done = function(){
this.throwing = true
if (this.state !== 'pending') {
this.fire()
}
return void 0
}
return void 0
}
this.throws = function(){
this.throwing = true
return this
}
this.throws = function(){
this.throwing = true
return this
}
this.catch = function (fn) {
this.stack.push([null, fn || function(){}])
if (this.state !== 'pending') {
this.catch = function (fn) {
this.stack.push([null, fn || function(){}])
if (this.state !== 'pending') {
this.fire()
}
return this
}
return this
}
this.fail = function (fn) {
return this.catch(fn)
}
this.fail = function (fn) {
return this.catch(fn)
}
this.nodeify = function () {
// TODO
}
this.nodeify = function (cb) {
if(cb) {
this.then(function(val){
nextTick(function(){
cb(null, val)
})
return val
}, function(err){
nextTick(function(){
cb(err)
})
})
}
return this
}
this.spread = function (fn, er) {
return this.all().then(function(list){
return fn ? fn.apply(void 0, list) : null
}, er)
}
this.spread = function (fn, er) {
return this.all().then(function(list){
return fn ? fn.apply(void 0, list) : null
}, er)
}
this.all = function () {
var self = this
this.all = function () {
var self = this
this.stack.push([function(list){
this.stack.push([function(list){
list = list instanceof Array ? list : []
list = list instanceof Array ? list : []
if (list.length === 0){
return list
}
if (list.length === 0){
return list
}
self.state = 'pending'
var cnt = 0
var errored = false
self.state = 'pending'
var cnt = 0
var errored = false
function checkDone(){
if(cnt !== list.length) {
return
function checkDone(){
if(cnt !== list.length) {
return
}
self.resolve(list)
}
self.resolve(list)
}
list.forEach(function(val, i){
if(val && val.then){
list.forEach(function(val, i){
if(val && val.then){
val.then(function(res){
list[i] = res
val.then(function(res){
list[i] = res
cnt++
checkDone()
}, function(err){
self.reject(err)
})
} else {
list[i] = val
cnt++
checkDone()
}, function(err){
self.reject(err)
})
} else {
list[i] = val
cnt++
checkDone()
}
}
})
})
return list
}, null])
return list
}, null])
if (this.state !== 'pending') {
if (this.state !== 'pending') {
this.fire()
}
return this
}
return this
}
this.fire = function(val){
val = this.value = typeof val !== 'undefined' ? val : this.value
this.fire = function(val){
val = this.value = typeof val !== 'undefined' ? val : this.value
while(this.stack.length && this.state !== 'pending') {
var entry = this.stack.shift()
var fn = this.state === 'rejected' ? entry[1] : entry[0]
while(this.stack.length && this.state !== 'pending') {
var entry = this.stack.shift()
var fn = this.state === 'rejected' ? entry[1] : entry[0]
if(fn) {
try {
if(fn) {
try {
val = this.value = fn.call(void 0, val)
if(val && val.then) {
var previousState = this.state
this.state = 'pending'
var promise = val.then(function(v){
val = this.value = fn.call(void 0, val)
if(val && val.then) {
var previousState = this.state
this.state = 'pending'
var promise = val.then(function(v){
val = this.value = v
val = this.value = v
this.resolve(v)
}.bind(this), function(err){
val = this.value = err
if(previousState !== 'rejected') this.stack.unshift(entry)
this.reject(err)
}.bind(this))
this.resolve(v)
}.bind(this), function(err){
val = this.value = err
if(previousState !== 'rejected') this.stack.unshift(entry)
this.reject(err)
}.bind(this))
} else if (this.state === 'rejected') {
this.state = 'resolved'
}
} catch (e) {
val = this.value = e
if(this.state !== 'rejected') this.stack.unshift(entry)
this.state = 'rejected'
} else if (this.state === 'rejected') {
this.state = 'resolved'
}
} catch (e) {
val = this.value = e
if(this.state !== 'rejected') this.stack.unshift(entry)
this.state = 'rejected'
}
}
}
if(this.throwing && this.stack.length === 0 && this.state === 'rejected') {
throw this.value
}
}
if(this.throwing && this.stack.length === 0 && this.state === 'rejected') {
throw this.value
}
}
}
return {
defer: function(){
return new defer()
},
return {
defer: function(){
return new defer()
},
fcall: function() {
var deferred = new defer()
var args = Array.apply([], arguments)
var fn = args.shift()
try {
var val = fn.apply(void 0, args)
deferred.resolve(val)
} catch(e) {
deferred.reject(e)
}
fcall: function() {
var deferred = new defer()
var args = Array.apply([], arguments)
var fn = args.shift()
deferred.resolve(fn.call(void 0, args))
return deferred
},
return deferred
},
nfcall: function() {
nfcall: function() {
var deferred = new defer()
var args = Array.apply([], arguments)
var fn = args.shift()
try {
args.push(function(err, val){
if(err) {
return deferred.reject(err)
}
return deferred.resolve(val)
})
fn.apply(void 0, args)
} catch(e){
deferred.reject(e)
}
},
return deferred
},
}
})()
if(typeof module !== 'undefined' && module.exports) {
module.exports = promiz
} else {
this.promiz = promiz
}
})()
module.exports = promiz

@@ -261,6 +261,121 @@ var promiz = require('./promiz')

it('is actually always asyncronouse', function(){
// WONT-FIX: Implementing this feature will slow down calls significantly in browsers
})
})
describe('nodeify', function(){
function testPromise() {
var deferred = promiz.defer()
process.nextTick(function(){
deferred.resolve(22)
})
return deferred
}
var promise = testPromise()
it('lets a promise terminate with a node-style callback', function(done){
function cb(err, val){
expect(val).toBe(3)
done()
}
promise.then(function(){
return 3
}).nodeify(cb)
})
it('terminates with a node style callback and passes errors down', function(done){
function cb(err, val){
expect(err.message).toBe('abc')
done()
}
promise.then(function(){
throw new Error('abc')
}).nodeify(cb)
})
it('still returns a promise to chain off of', function(done){
testPromise().then(function(){
return 3
}).nodeify(function(){}).then(function(val){
expect(val).toBe(3)
return 11
}).nodeify().then(function(val){
expect(val).toBe(11)
done()
})
})
})
describe('fcall', function(){
it('calls a function and returns a promise', function(done){
promiz.fcall(function(a, two, tr){
expect(a).toBe('a')
expect(two).toBe(2)
expect(tr).toBe(true)
return [a, two, tr]
}, 'a', 2, true).then(function(list){
expect(list.length).toBe(3)
done()
})
})
it('properly handles errors thrown by the function', function(done){
promiz.fcall(function(){
throw new Error('abc')
}).then(function(){
done(new Error('fcall did catch throw properly'))
}).catch(function(err){
expect(err.message).toBe('abc')
done()
})
})
})
describe('nfcall', function(){
it('calls a node-style function and returns a promise', function(done){
function nodeStyle(val1, val2, val3, cb){
expect(val1).toBe('a')
expect(val2).toBe(2)
expect(val3).toBe(true)
process.nextTick(function(){
cb(null, 88)
})
}
promiz.nfcall(nodeStyle, 'a', 2, true).then(function(val){
expect(val).toBe(88)
done()
})
})
it('properly handles errors returned by the function', function(done){
function nodeStyle(cb){
process.nextTick(function(){
cb(new Error('abc'))
})
}
promiz.nfcall(nodeStyle).then(function(){
done(new Error('nfcall did not catch throw properly'))
}).fail(function(err){
expect(err.message).toBe('abc')
done()
})
})
it('properly handles errors thrown by the function', function(){
function nodeStyle(cb){
throw new Error('abc')
}
promiz.nfcall(nodeStyle).then(function(){
done(new Error('nfcall did not catch throw properly'))
}).fail(function(err){
expect(err.message).toBe('abc')
done()
})
})
})
})

@@ -267,0 +382,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc