Skip to content

Commit 6d013fe

Browse files
committed
Allow ES6-style Promise construction
1 parent 96e211d commit 6d013fe

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/ParsePromise.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ var isPromisesAPlusCompliant = true;
2626
* @constructor
2727
*/
2828
export default class ParsePromise {
29-
constructor() {
29+
constructor(closure) {
3030
this._resolved = false;
3131
this._rejected = false;
3232
this._resolvedCallbacks = [];
3333
this._rejectedCallbacks = [];
34+
35+
if (typeof closure === 'function') {
36+
closure(this.resolve.bind(this), this.reject.bind(this));
37+
}
3438
}
3539

3640
/**

src/__tests__/ParsePromise-test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,5 +430,37 @@ describe('Promise', () => {
430430
expect(ParsePromise.is({})).toBe(false);
431431
expect(ParsePromise.is(ParsePromise.as())).toBe(true);
432432
expect(ParsePromise.is(ParsePromise.error())).toBe(true);
433-
})
433+
});
434+
435+
it('can be constructed in ES6 style and resolved', asyncHelper((done) => {
436+
new ParsePromise((resolve, reject) => {
437+
resolve('abc');
438+
}).then((result) => {
439+
expect(result).toBe('abc');
440+
441+
return new ParsePromise((resolve, reject) => {
442+
resolve('def');
443+
});
444+
}).then((result) => {
445+
expect(result).toBe('def');
446+
done();
447+
});
448+
}));
449+
450+
it('can be constructed in ES6 style and rejected', asyncHelper((done) => {
451+
new ParsePromise((resolve, reject) => {
452+
reject('err');
453+
}).then(() => {
454+
// Should not be reached
455+
}, (error) => {
456+
expect(error).toBe('err');
457+
458+
return new ParsePromise((resolve, reject) => {
459+
reject('err2');
460+
});
461+
}).then(null, (error) => {
462+
expect(error).toBe('err2');
463+
done();
464+
});
465+
}));
434466
});

0 commit comments

Comments
 (0)