本文整理汇总了TypeScript中rxjs.Observable.fromPromise方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Observable.fromPromise方法的具体用法?TypeScript Observable.fromPromise怎么用?TypeScript Observable.fromPromise使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rxjs.Observable
的用法示例。
在下文中一共展示了Observable.fromPromise方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: factory
function createObservable<T>(factory: () => Promise<T>): Observable<T> {
const topic: Promise<T> = factory();
if (topic instanceof Promise) {
const lazyRec: () => Observable<T> = () => {
return Observable.fromPromise(factory()).concat(
Observable.timer(60 * 60 * 1000).flatMap(() => lazyRec()));
};
return Observable.fromPromise(topic).concat(
Observable.timer(60 * 60 * 1000).flatMap(() => lazyRec()));
} else {
return Observable.fromPromise(topic);
}
}
示例2: getProjectMembers
getProjectMembers(projectId: string): Rx.Observable<Member[]> {
const get = MemberModel.getProjectMembers(projectId)
if (get) return get
return Rx.Observable
.fromPromise(memberFetch.getProjectMembers(projectId))
.concatMap(x => MemberModel.saveProjectMembers(projectId, x))
}
示例3:
this.accounts$ = Observable.defer(() => {
const promise = API.one('credentials')
.useCache()
.withParams({ expand: true })
.get();
return Observable.fromPromise<IAccountDetails[]>(promise);
})
示例4: connect
public connect(): Observable<Channel> {
if (!this._uri) {
return Observable.throw(
new Error('Uri must be defined in order to start connection')
);
}
const self: RPC_Client = this;
// Connect to RabbitMQ
const connPromise: any = connect(this.uri)
// Assign Connection and Create Channel
.then((connection: Connection) => {
self._connection = connection;
return connection.createChannel();
})
// Assign Channel and create consume listener
.then((channel: Channel) => {
self._channel = channel;
channel.setMaxListeners(0);
// Action to take when message received
self.consumeChannel.apply(self, [channel]);
return channel;
});
return Observable.fromPromise(connPromise);
}
示例5: addToIndex
addToIndex(value): Observable<any> {
var client = new elasticsearch.Client({
host: 'http://localhost:9200',
log: 'trace'
});
return Observable.fromPromise(client.create(value))
}
示例6: rpcPush
public rpcPush(queueName: string, message: any): Observable<{content: any}> {
if (!this.connection || !this.channel) {
return Observable.throw(
new Error('A connection is required to send messages.')
);
}
if (typeof message !== 'object') {
return Observable.throw(
new Error('Message must be a JSON object.')
);
}
const self: RPC_Client = this;
return Observable.fromPromise<any>(
new Promise(
(resolve: Function) => {
// Generate message id
const correlationId: string = self.generateUuid();
// Create listener for response
self.responseEmitter.once(correlationId, resolve);
// Send message
self.sendMessageToQueue.apply(self, [queueName, message, correlationId]);
}
)
.then((msg: Buffer) => JSON.parse(msg.toString()))
);
}
示例7: getTTSAccessToken
// 百度TTS 合成token
getTTSAccessToken()
{
let url = "https://openapi.baidu.com/oauth/2.0/token";
let params = {
"grant_type": "client_credentials",
"client_id": "TPnGgO8zxIayL9iTTctlxqny",
"client_secret": "820591dad6912546574487991d6bcb7d"
};
if(this.platform.is("cordova"))
{
return Observable.fromPromise(this.nativeHttp.get(url, params, {})).map((res:any) =>{
return JSON.parse(res.data);
}).catch(this.handleError);
}
else
{
url = url.replace("https:/", "");
let searchParams = new URLSearchParams();
for(let key in params)
{
searchParams.set(key, params[key]);
}
let reqOpts = new RequestOptions({"search": searchParams, "headers": new Headers()});
return this.http.get(url, reqOpts).map(res=>{
return res.json();
}).catch(this.handleError);
}
}
示例8: it
it('Rx.Observable asynchronous test', function () {
assert.strictEqual(count++, 10)
return Rx.Observable.fromPromise(new Promise(function (resolve) {
assert.strictEqual(count++, 11)
setTimeout(resolve, 100)
}))
})
示例9: execute
execute() {
if (this._watcher) {
throw new Error('Already started');
}
return Observable.fromPromise(this.createAction())
.flatMap(job => {
if (this._runner === undefined) {
const runner = new WatchExecutor(job, this._dialog);
this._runner = runner;
// FIXME how to stop the runner
const cbk = _.debounce(path => runner.run(), this._debounceTime);
this._watcher = chokidar.watch(this._def.files, {
// ignored: /../,
// persistent: true
});
this._watcher.on('error', err => {
console.error('Error during watch', err);
});
this._watcher.on('ready', () => {
LISTEN_EVENTS.forEach(eventName => this._watcher.on(eventName, cbk));
});
return runner.subject;
} else {
return this._runner.subject;
}
});
}
示例10: getOrgMembers
getOrgMembers (organizationId: string): Rx.Observable<Member[]> {
const get = MemberModel.getOrgMembers(organizationId)
if (get) return get
return Rx.Observable
.fromPromise(memberFetch.getOrgMembers(organizationId))
.concatMap(x => MemberModel.saveOrgMembers(organizationId, x))
}