Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/ParsePromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ var isPromisesAPlusCompliant = true;
* @constructor
*/
export default class ParsePromise {
constructor() {
constructor(executor) {
this._resolved = false;
this._rejected = false;
this._resolvedCallbacks = [];
this._rejectedCallbacks = [];

if (typeof executor === 'function') {
executor(this.resolve.bind(this), this.reject.bind(this));
}
}

/**
Expand Down Expand Up @@ -301,6 +305,26 @@ export default class ParsePromise {
return promise;
}

/**
* Returns a new promise that is resolved with a given value.
* If that value is a thenable Promise (has a .then() prototype
* method), the new promise will be chained to the end of the
* value.
* @method resolve
* @param value The value to resolve the promise with
* @static
* @return {Parse.Promise} the new promise.
*/
static resolve(value) {
return new ParsePromise((resolve, reject) => {
if (ParsePromise.is(value)) {
value.then(resolve, reject);
} else {
resolve(value);
}
});
}

/**
* Returns a new promise that is rejected with a given error.
* @method error
Expand All @@ -314,6 +338,19 @@ export default class ParsePromise {
return promise;
}

/**
* Returns a new promise that is rejected with a given error.
* This is an alias for Parse.Promise.error, for compliance with
* the ES6 implementation.
* @method reject
* @param error The error to reject the promise with
* @static
* @return {Parse.Promise} the new promise.
*/
static reject(...errors) {
return ParsePromise.error.apply(null, errors);
}

/**
* Returns a new promise that is fulfilled when all of the input promises
* are resolved. If any promise in the list fails, then the returned promise
Expand Down
54 changes: 53 additions & 1 deletion src/__tests__/ParsePromise-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,5 +430,57 @@ describe('Promise', () => {
expect(ParsePromise.is({})).toBe(false);
expect(ParsePromise.is(ParsePromise.as())).toBe(true);
expect(ParsePromise.is(ParsePromise.error())).toBe(true);
})
});

it('can be constructed in ES6 style and resolved', asyncHelper((done) => {
expect(ParsePromise.length).toBe(1); // constructor arguments

new ParsePromise((resolve, reject) => {
resolve('abc');
}).then((result) => {
expect(result).toBe('abc');

return new ParsePromise((resolve, reject) => {
resolve('def');
});
}).then((result) => {
expect(result).toBe('def');
done();
});
}));

it('can be constructed in ES6 style and rejected', asyncHelper((done) => {
new ParsePromise((resolve, reject) => {
reject('err');
}).then(() => {
// Should not be reached
}, (error) => {
expect(error).toBe('err');

return new ParsePromise((resolve, reject) => {
reject('err2');
});
}).then(null, (error) => {
expect(error).toBe('err2');
done();
});
}));

it('can be initially resolved, ES6 style', asyncHelper((done) => {
ParsePromise.resolve('abc').then((result) => {
expect(result).toBe('abc');

return ParsePromise.resolve(ParsePromise.as('def'));
}).then((result) => {
expect(result).toBe('def');
done();
});
}));

it('can be initially rejected, ES6 style', asyncHelper((done) => {
ParsePromise.reject('err').then(null, (error) => {
expect(error).toBe('err');
done();
});
}));
});