Skip to content

Commit 4a09e26

Browse files
committed
Add ES6 shortcut constructors
1 parent 6d013fe commit 4a09e26

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/ParsePromise.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,26 @@ export default class ParsePromise {
305305
return promise;
306306
}
307307

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+
308328
/**
309329
* Returns a new promise that is rejected with a given error.
310330
* @method error
@@ -318,6 +338,19 @@ export default class ParsePromise {
318338
return promise;
319339
}
320340

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+
321354
/**
322355
* Returns a new promise that is fulfilled when all of the input promises
323356
* are resolved. If any promise in the list fails, then the returned promise

src/__tests__/ParsePromise-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ describe('Promise', () => {
433433
});
434434

435435
it('can be constructed in ES6 style and resolved', asyncHelper((done) => {
436+
expect(ParsePromise.length).toBe(1); // constructor arguments
437+
436438
new ParsePromise((resolve, reject) => {
437439
resolve('abc');
438440
}).then((result) => {
@@ -463,4 +465,22 @@ describe('Promise', () => {
463465
done();
464466
});
465467
}));
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+
}));
466486
});

0 commit comments

Comments
 (0)