本文整理匯總了TypeScript中rsvp.Promise類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Promise類的具體用法?TypeScript Promise怎麽用?TypeScript Promise使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Promise類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: testHashSettled
function testHashSettled() {
function isFulfilled<T>(state: RSVP.PromiseState<T>): state is RSVP.Resolved<T> {
return state.state === RSVP.State.fulfilled;
}
let promises = {
myPromise: RSVP.Promise.resolve(1),
yourPromise: RSVP.Promise.resolve('2'),
theirPromise: RSVP.Promise.resolve({ key: 3 }),
notAPromise: 4,
};
RSVP.hashSettled(promises).then(function(hash) {
if (isFulfilled(hash.myPromise)) {
assertType<number>(hash.myPromise.value);
}
if (isFulfilled(hash.yourPromise)) {
assertType<string>(hash.yourPromise.value);
}
if (isFulfilled(hash.theirPromise)) {
assertType<{ key: number }>(hash.theirPromise.value);
}
if (isFulfilled(hash.notAPromise)) {
assertType<number>(hash.notAPromise.value);
}
});
}
示例2: testResolve
function testResolve() {
assertType<RSVP.Promise<void>>(RSVP.resolve());
assertType<RSVP.Promise<string>>(RSVP.resolve('this is a string'));
assertType<RSVP.Promise<string>>(RSVP.resolve(RSVP.resolve('nested')));
assertType<RSVP.Promise<string>>(RSVP.resolve(Promise.resolve('nested')));
let promise = RSVP.Promise.resolve(1);
let imported = resolve(1);
}
示例3: testCast
function testCast() {
RSVP.Promise.cast('foo').then(value => {
assertType<string>(value);
});
RSVP.cast(42).then(value => {
assertType<number>(value);
});
}
示例4: TransitionState
.then(function(result: TransitionState<Route>) {
let models = [];
for (let i = 0; i < result.routeInfos.length; i++) {
models.push(result.routeInfos[i].context);
}
assert.equal(models[0], fooModel);
assert.equal(models[1], barModel);
return Promise.resolve(new TransitionState());
})
示例5: testReject
function testReject() {
assertType<RSVP.Promise<never>>(RSVP.reject());
assertType<RSVP.Promise<never>>(RSVP.reject('this is a string'));
RSVP.reject({ ok: false }).catch(reason => {
console.log(`${reason} could be anything`);
});
RSVP.reject({ ok: false }, 'some label').catch((reason: any) => reason.ok);
let promise = RSVP.Promise.reject(new Error('WHOOPS'));
}
示例6: testRace
function testRace() {
const imported = race([]);
const firstPromise = RSVP.race([{ notAPromise: true }, RSVP.resolve({ some: 'value' })]);
assertType<RSVP.Promise<{ notAPromise: boolean } | { some: string }>>(firstPromise);
let promise1 = RSVP.resolve(1);
let promise2 = RSVP.resolve('2');
RSVP.Promise.race([promise1, promise2], 'my label').then(function(result) {
assertType<string | number>(result);
});
}
示例7:
(() => {
if (err === null) {
return this.pageModel.ajax<any>(
'POST',
this.pageModel.createActionUrl('/subcorpus/subcorp', [['format', 'json']]),
args
);
} else {
return RSVP.Promise.reject(err);
}
})().then(
示例8: submit
submit():RSVP.Promise<any> {
const args = this.getSubmitArgs();
const err = this.validateForm(true);
if (err === null) {
return this.pageModel.ajax<any>(
'POST',
this.pageModel.createActionUrl('/subcorpus/subcorp', [['format', 'json']]),
args
);
} else {
return RSVP.Promise.reject(err);
}
}
示例9: testAll
function testAll() {
const imported = all([]);
const empty = RSVP.Promise.all([]);
const everyPromise = RSVP.all([
'string',
RSVP.resolve(42),
RSVP.resolve({ hash: 'with values' }),
]);
assertType<RSVP.Promise<[string, number, { hash: string }]>>(everyPromise);
const anyFailure = RSVP.all([12, 'strings', RSVP.reject('anywhere')]);
assertType<RSVP.Promise<{}>>(anyFailure);
let promise1 = RSVP.resolve(1);
let promise2 = RSVP.resolve('2');
let promise3 = RSVP.resolve({ key: 13 });
RSVP.Promise.all([promise1, promise2, promise3], 'my label').then(function(array) {
assertType<number>(array[0]);
assertType<string>(array[1]);
assertType<{ key: number }>(array[2]);
});
}
示例10: present
/**
Creates an "owner" (an object that either _is_ or duck-types like an
`Ember.ApplicationInstance`) from the provided options.
If `options.application` is present (e.g. setup by an earlier call to
`setApplication`) an `Ember.ApplicationInstance` is built via
`application.buildInstance()`.
If `options.application` is not present, we fall back to using
`options.resolver` instead (setup via `setResolver`). This creates a mock
"owner" by using a custom created combination of `Ember.Registry`,
`Ember.Container`, `Ember._ContainerProxyMixin`, and
`Ember._RegistryProxyMixin`.
@private
@param {Ember.Application} [application] the Ember.Application to build an instance from
@param {Ember.Resolver} [resolver] the resolver to use to back a "mock owner"
@returns {Promise<Ember.ApplicationInstance>} a promise resolving to the generated "owner"
*/
export default function buildOwner(
application: Application | undefined | null,
resolver: Resolver | undefined | null
): Promise<Owner> {
if (application) {
return (application.boot().then(app => app.buildInstance().boot()) as unknown) as Promise<
Owner
>;
}
if (!resolver) {
throw new Error(
'You must set up the ember-test-helpers environment with either `setResolver` or `setApplication` before running any tests.'
);
}
let { owner } = legacyBuildRegistry(resolver) as { owner: Owner };
return Promise.resolve(owner);
}