Skip to content

Commit 0498f7c

Browse files
author
Forbes Lindesay
committed
Split out the different extensions
The different extensions fit into different categories. This should make it easier to chose your extensions.
1 parent 0d29e09 commit 0498f7c

File tree

5 files changed

+187
-184
lines changed

5 files changed

+187
-184
lines changed

index.js

Lines changed: 4 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1,185 +1,6 @@
11
'use strict';
22

3-
//This file contains then/promise specific extensions to the core promise API
4-
5-
var Promise = require('./core.js')
6-
var asap = require('asap')
7-
8-
module.exports = Promise
9-
10-
/* Static Functions */
11-
12-
function ValuePromise(value) {
13-
this.then = function (onFulfilled) {
14-
if (typeof onFulfilled !== 'function') return this
15-
return new Promise(function (resolve, reject) {
16-
asap(function () {
17-
try {
18-
resolve(onFulfilled(value))
19-
} catch (ex) {
20-
reject(ex);
21-
}
22-
})
23-
})
24-
}
25-
}
26-
ValuePromise.prototype = Promise.prototype
27-
28-
var TRUE = new ValuePromise(true)
29-
var FALSE = new ValuePromise(false)
30-
var NULL = new ValuePromise(null)
31-
var UNDEFINED = new ValuePromise(undefined)
32-
var ZERO = new ValuePromise(0)
33-
var EMPTYSTRING = new ValuePromise('')
34-
35-
Promise.resolve = function (value) {
36-
if (value instanceof Promise) return value
37-
38-
if (value === null) return NULL
39-
if (value === undefined) return UNDEFINED
40-
if (value === true) return TRUE
41-
if (value === false) return FALSE
42-
if (value === 0) return ZERO
43-
if (value === '') return EMPTYSTRING
44-
45-
if (typeof value === 'object' || typeof value === 'function') {
46-
try {
47-
var then = value.then
48-
if (typeof then === 'function') {
49-
return new Promise(then.bind(value))
50-
}
51-
} catch (ex) {
52-
return new Promise(function (resolve, reject) {
53-
reject(ex)
54-
})
55-
}
56-
}
57-
58-
return new ValuePromise(value)
59-
}
60-
61-
Promise.from = Promise.cast = function (value) {
62-
var err = new Error('Promise.from and Promise.cast are deprecated, use Promise.resolve instead')
63-
err.name = 'Warning'
64-
console.warn(err.stack)
65-
return Promise.resolve(value)
66-
}
67-
68-
Promise.denodeify = function (fn, argumentCount) {
69-
argumentCount = argumentCount || Infinity
70-
return function () {
71-
var self = this
72-
var args = Array.prototype.slice.call(arguments)
73-
return new Promise(function (resolve, reject) {
74-
while (args.length && args.length > argumentCount) {
75-
args.pop()
76-
}
77-
args.push(function (err, res) {
78-
if (err) reject(err)
79-
else resolve(res)
80-
})
81-
fn.apply(self, args)
82-
})
83-
}
84-
}
85-
Promise.nodeify = function (fn) {
86-
return function () {
87-
var args = Array.prototype.slice.call(arguments)
88-
var callback = typeof args[args.length - 1] === 'function' ? args.pop() : null
89-
var ctx = this
90-
try {
91-
return fn.apply(this, arguments).nodeify(callback, ctx)
92-
} catch (ex) {
93-
if (callback === null || typeof callback == 'undefined') {
94-
return new Promise(function (resolve, reject) { reject(ex) })
95-
} else {
96-
asap(function () {
97-
callback.call(ctx, ex)
98-
})
99-
}
100-
}
101-
}
102-
}
103-
104-
Promise.all = function (arr) {
105-
if (arguments.length !== 1 || !Array.isArray(arr))
106-
return variadicAll.apply(this, arguments)
107-
108-
var args = Array.prototype.slice.call(arr)
109-
110-
return new Promise(function (resolve, reject) {
111-
if (args.length === 0) return resolve([])
112-
var remaining = args.length
113-
function res(i, val) {
114-
try {
115-
if (val && (typeof val === 'object' || typeof val === 'function')) {
116-
var then = val.then
117-
if (typeof then === 'function') {
118-
then.call(val, function (val) { res(i, val) }, reject)
119-
return
120-
}
121-
}
122-
args[i] = val
123-
if (--remaining === 0) {
124-
resolve(args);
125-
}
126-
} catch (ex) {
127-
reject(ex)
128-
}
129-
}
130-
for (var i = 0; i < args.length; i++) {
131-
res(i, args[i])
132-
}
133-
})
134-
}
135-
136-
function variadicAll() {
137-
var err = new Error('Promise.all should be called with a single array, calling it with multiple arguments is deprecated')
138-
err.name = 'Warning'
139-
console.warn(err.stack)
140-
141-
return Promise.all(Array.prototype.slice.call(arguments))
142-
}
143-
144-
Promise.reject = function (value) {
145-
return new Promise(function (resolve, reject) {
146-
reject(value);
147-
});
148-
}
149-
150-
Promise.race = function (values) {
151-
return new Promise(function (resolve, reject) {
152-
values.forEach(function(value){
153-
Promise.resolve(value).then(resolve, reject);
154-
})
155-
});
156-
}
157-
158-
/* Prototype Methods */
159-
160-
Promise.prototype.done = function (onFulfilled, onRejected) {
161-
var self = arguments.length ? this.then.apply(this, arguments) : this
162-
self.then(null, function (err) {
163-
asap(function () {
164-
throw err
165-
})
166-
})
167-
}
168-
169-
Promise.prototype.nodeify = function (callback, ctx) {
170-
if (typeof callback != 'function') return this
171-
172-
this.then(function (value) {
173-
asap(function () {
174-
callback.call(ctx, null, value)
175-
})
176-
}, function (err) {
177-
asap(function () {
178-
callback.call(ctx, err)
179-
})
180-
})
181-
}
182-
183-
Promise.prototype['catch'] = function (onRejected) {
184-
return this.then(null, onRejected);
185-
}
3+
module.exports = require('./lib/core.js')
4+
require('./lib/done.js')
5+
require('./lib/es6-extensions.js')
6+
require('./lib/node-extensions.js')

core.js renamed to lib/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var asap = require('asap')
44

5-
module.exports = Promise
5+
module.exports = Promise;
66
function Promise(fn) {
77
if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new')
88
if (typeof fn !== 'function') throw new TypeError('not a function')

lib/done.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
var Promise = require('./core.js')
4+
var asap = require('asap')
5+
6+
module.exports = Promise
7+
Promise.prototype.done = function (onFulfilled, onRejected) {
8+
var self = arguments.length ? this.then.apply(this, arguments) : this
9+
self.then(null, function (err) {
10+
asap(function () {
11+
throw err
12+
})
13+
})
14+
}

lib/es6-extensions.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
'use strict';
2+
3+
//This file contains the ES6 extensions to the core Promises/A+ API
4+
5+
var Promise = require('./core.js')
6+
var asap = require('asap')
7+
8+
module.exports = Promise
9+
10+
/* Static Functions */
11+
12+
function ValuePromise(value) {
13+
this.then = function (onFulfilled) {
14+
if (typeof onFulfilled !== 'function') return this
15+
return new Promise(function (resolve, reject) {
16+
asap(function () {
17+
try {
18+
resolve(onFulfilled(value))
19+
} catch (ex) {
20+
reject(ex);
21+
}
22+
})
23+
})
24+
}
25+
}
26+
ValuePromise.prototype = Promise.prototype
27+
28+
var TRUE = new ValuePromise(true)
29+
var FALSE = new ValuePromise(false)
30+
var NULL = new ValuePromise(null)
31+
var UNDEFINED = new ValuePromise(undefined)
32+
var ZERO = new ValuePromise(0)
33+
var EMPTYSTRING = new ValuePromise('')
34+
35+
Promise.resolve = function (value) {
36+
if (value instanceof Promise) return value
37+
38+
if (value === null) return NULL
39+
if (value === undefined) return UNDEFINED
40+
if (value === true) return TRUE
41+
if (value === false) return FALSE
42+
if (value === 0) return ZERO
43+
if (value === '') return EMPTYSTRING
44+
45+
if (typeof value === 'object' || typeof value === 'function') {
46+
try {
47+
var then = value.then
48+
if (typeof then === 'function') {
49+
return new Promise(then.bind(value))
50+
}
51+
} catch (ex) {
52+
return new Promise(function (resolve, reject) {
53+
reject(ex)
54+
})
55+
}
56+
}
57+
58+
return new ValuePromise(value)
59+
}
60+
61+
Promise.all = function (arr) {
62+
var args = Array.prototype.slice.call(arr)
63+
64+
return new Promise(function (resolve, reject) {
65+
if (args.length === 0) return resolve([])
66+
var remaining = args.length
67+
function res(i, val) {
68+
try {
69+
if (val && (typeof val === 'object' || typeof val === 'function')) {
70+
var then = val.then
71+
if (typeof then === 'function') {
72+
then.call(val, function (val) { res(i, val) }, reject)
73+
return
74+
}
75+
}
76+
args[i] = val
77+
if (--remaining === 0) {
78+
resolve(args);
79+
}
80+
} catch (ex) {
81+
reject(ex)
82+
}
83+
}
84+
for (var i = 0; i < args.length; i++) {
85+
res(i, args[i])
86+
}
87+
})
88+
}
89+
90+
Promise.reject = function (value) {
91+
return new Promise(function (resolve, reject) {
92+
reject(value);
93+
});
94+
}
95+
96+
Promise.race = function (values) {
97+
return new Promise(function (resolve, reject) {
98+
values.forEach(function(value){
99+
Promise.resolve(value).then(resolve, reject);
100+
})
101+
});
102+
}
103+
104+
/* Prototype Methods */
105+
106+
Promise.prototype['catch'] = function (onRejected) {
107+
return this.then(null, onRejected);
108+
}

lib/node-extensions.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
//This file contains then/promise specific extensions that are only useful for node.js interop
4+
5+
var Promise = require('./core.js')
6+
var asap = require('asap')
7+
8+
module.exports = Promise
9+
10+
/* Static Functions */
11+
12+
Promise.denodeify = function (fn, argumentCount) {
13+
argumentCount = argumentCount || Infinity
14+
return function () {
15+
var self = this
16+
var args = Array.prototype.slice.call(arguments)
17+
return new Promise(function (resolve, reject) {
18+
while (args.length && args.length > argumentCount) {
19+
args.pop()
20+
}
21+
args.push(function (err, res) {
22+
if (err) reject(err)
23+
else resolve(res)
24+
})
25+
fn.apply(self, args)
26+
})
27+
}
28+
}
29+
Promise.nodeify = function (fn) {
30+
return function () {
31+
var args = Array.prototype.slice.call(arguments)
32+
var callback = typeof args[args.length - 1] === 'function' ? args.pop() : null
33+
var ctx = this
34+
try {
35+
return fn.apply(this, arguments).nodeify(callback, ctx)
36+
} catch (ex) {
37+
if (callback === null || typeof callback == 'undefined') {
38+
return new Promise(function (resolve, reject) { reject(ex) })
39+
} else {
40+
asap(function () {
41+
callback.call(ctx, ex)
42+
})
43+
}
44+
}
45+
}
46+
}
47+
48+
Promise.prototype.nodeify = function (callback, ctx) {
49+
if (typeof callback != 'function') return this
50+
51+
this.then(function (value) {
52+
asap(function () {
53+
callback.call(ctx, null, value)
54+
})
55+
}, function (err) {
56+
asap(function () {
57+
callback.call(ctx, err)
58+
})
59+
})
60+
}

0 commit comments

Comments
 (0)