-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): add toPromise operator. closes #159
- Loading branch information
Showing
4 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var promise = require('promise'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.prototype.toPromise()', function () { | ||
it('should convert an Observable to a promise of its last value', function (done) { | ||
Observable.of(1, 2, 3).toPromise(promise).then(function (x) { | ||
expect(x).toBe(3); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should handle errors properly', function (done) { | ||
Observable.throw('bad').toPromise(promise).then(function () { | ||
throw 'should not be called'; | ||
}, function (err) { | ||
expect(err).toBe('bad'); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Subscriber from '../Subscriber'; | ||
|
||
export default function toPromise<T>(PromiseCtor: PromiseConstructor = Promise): Promise<T> { | ||
return new PromiseCtor((resolve, reject) => { | ||
let value: any; | ||
this.subscribe(x => value = x, err => reject(err), () => resolve(value)); | ||
}); | ||
} |