Skip to content

Commit 5e5c444

Browse files
committed
Merge pull request #154 from ParsePlatform/andrewi.promise_constructor
Allow ES6-style Promise construction
2 parents 96e211d + 699561c commit 5e5c444

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

src/ParsePromise.js

Lines changed: 38 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(executor) {
3030
this._resolved = false;
3131
this._rejected = false;
3232
this._resolvedCallbacks = [];
3333
this._rejectedCallbacks = [];
34+
35+
if (typeof executor === 'function') {
36+
executor(this.resolve.bind(this), this.reject.bind(this));
37+
}
3438
}
3539

3640
/**
@@ -301,6 +305,26 @@ export default class ParsePromise {
301305
return promise;
302306
}
303307

308+
/**
309+
* Returns a new promise that is resolved with a given value.
310+
* If that value is a thenable Promise (has a .then() prototype
311+
* method), the new promise will be chained to the end of the
312+
* value.
313+
* @method resolve
314+
* @param value The value to resolve the promise with
315+
* @static
316+
* @return {Parse.Promise} the new promise.
317+
*/
318+
static resolve(value) {
319+
return new ParsePromise((resolve, reject) => {
320+
if (ParsePromise.is(value)) {
321+
value.then(resolve, reject);
322+
} else {
323+
resolve(value);
324+
}
325+
});
326+
}
327+
304328
/**
305329
* Returns a new promise that is rejected with a given error.
306330
* @method error
@@ -314,6 +338,19 @@ export default class ParsePromise {
314338
return promise;
315339
}
316340

341+
/**
342+
* Returns a new promise that is rejected with a given error.
343+
* This is an alias for Parse.Promise.error, for compliance with
344+
* the ES6 implementation.
345+
* @method reject
346+
* @param error The error to reject the promise with
347+
* @static
348+
* @return {Parse.Promise} the new promise.
349+
*/
350+
static reject(...errors) {
351+
return ParsePromise.error.apply(null, errors);
352+
}
353+
317354
/**
318355
* Returns a new promise that is fulfilled when all of the input promises
319356
* are resolved. If any promise in the list fails, then the returned promise

src/__tests__/ParsePromise-test.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,5 +430,57 @@ 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+
expect(ParsePromise.length).toBe(1); // constructor arguments
437+
438+
new ParsePromise((resolve, reject) => {
439+
resolve('abc');
440+
}).then((result) => {
441+
expect(result).toBe('abc');
442+
443+
return new ParsePromise((resolve, reject) => {
444+
resolve('def');
445+
});
446+
}).then((result) => {
447+
expect(result).toBe('def');
448+
done();
449+
});
450+
}));
451+
452+
it('can be constructed in ES6 style and rejected', asyncHelper((done) => {
453+
new ParsePromise((resolve, reject) => {
454+
reject('err');
455+
}).then(() => {
456+
// Should not be reached
457+
}, (error) => {
458+
expect(error).toBe('err');
459+
460+
return new ParsePromise((resolve, reject) => {
461+
reject('err2');
462+
});
463+
}).then(null, (error) => {
464+
expect(error).toBe('err2');
465+
done();
466+
});
467+
}));
468+
469+
it('can be initially resolved, ES6 style', asyncHelper((done) => {
470+
ParsePromise.resolve('abc').then((result) => {
471+
expect(result).toBe('abc');
472+
473+
return ParsePromise.resolve(ParsePromise.as('def'));
474+
}).then((result) => {
475+
expect(result).toBe('def');
476+
done();
477+
});
478+
}));
479+
480+
it('can be initially rejected, ES6 style', asyncHelper((done) => {
481+
ParsePromise.reject('err').then(null, (error) => {
482+
expect(error).toBe('err');
483+
done();
484+
});
485+
}));
434486
});

0 commit comments

Comments
 (0)