Skip to content

Commit 5f2511e

Browse files
Merge pull request then#49 from then/split-extensions
Split extensions
2 parents 69df615 + 656fad0 commit 5f2511e

File tree

9 files changed

+316
-286
lines changed

9 files changed

+316
-286
lines changed

core.js

Lines changed: 2 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,5 @@
11
'use strict';
22

3-
var asap = require('asap')
3+
module.exports = require('./lib/core.js');
44

5-
module.exports = Promise
6-
function Promise(fn) {
7-
if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new')
8-
if (typeof fn !== 'function') throw new TypeError('not a function')
9-
var state = null
10-
var value = null
11-
var deferreds = []
12-
var self = this
13-
14-
this.then = function(onFulfilled, onRejected) {
15-
return new self.constructor(function(resolve, reject) {
16-
handle(new Handler(onFulfilled, onRejected, resolve, reject))
17-
})
18-
}
19-
20-
function handle(deferred) {
21-
if (state === null) {
22-
deferreds.push(deferred)
23-
return
24-
}
25-
asap(function() {
26-
var cb = state ? deferred.onFulfilled : deferred.onRejected
27-
if (cb === null) {
28-
(state ? deferred.resolve : deferred.reject)(value)
29-
return
30-
}
31-
var ret
32-
try {
33-
ret = cb(value)
34-
}
35-
catch (e) {
36-
deferred.reject(e)
37-
return
38-
}
39-
deferred.resolve(ret)
40-
})
41-
}
42-
43-
function resolve(newValue) {
44-
try { //Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure
45-
if (newValue === self) throw new TypeError('A promise cannot be resolved with itself.')
46-
if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) {
47-
var then = newValue.then
48-
if (typeof then === 'function') {
49-
doResolve(then.bind(newValue), resolve, reject)
50-
return
51-
}
52-
}
53-
state = true
54-
value = newValue
55-
finale()
56-
} catch (e) { reject(e) }
57-
}
58-
59-
function reject(newValue) {
60-
state = false
61-
value = newValue
62-
finale()
63-
}
64-
65-
function finale() {
66-
for (var i = 0, len = deferreds.length; i < len; i++)
67-
handle(deferreds[i])
68-
deferreds = null
69-
}
70-
71-
doResolve(fn, resolve, reject)
72-
}
73-
74-
75-
function Handler(onFulfilled, onRejected, resolve, reject){
76-
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null
77-
this.onRejected = typeof onRejected === 'function' ? onRejected : null
78-
this.resolve = resolve
79-
this.reject = reject
80-
}
81-
82-
/**
83-
* Take a potentially misbehaving resolver function and make sure
84-
* onFulfilled and onRejected are only called once.
85-
*
86-
* Makes no guarantees about asynchrony.
87-
*/
88-
function doResolve(fn, onFulfilled, onRejected) {
89-
var done = false;
90-
try {
91-
fn(function (value) {
92-
if (done) return
93-
done = true
94-
onFulfilled(value)
95-
}, function (reason) {
96-
if (done) return
97-
done = true
98-
onRejected(reason)
99-
})
100-
} catch (ex) {
101-
if (done) return
102-
done = true
103-
onRejected(ex)
104-
}
105-
}
5+
console.error('require("promise/core") is deprecated, use require("promise/lib/core") instead.');

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')

0 commit comments

Comments
 (0)