本文整理匯總了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))
}