mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 06:47:03 +00:00
es6-promise: Provides its own types (#22420)
This commit is contained in:
@@ -318,6 +318,12 @@
|
||||
"sourceRepoURL": "https://github.com/bjyoungblood/es6-error",
|
||||
"asOfVersion": "4.0.2"
|
||||
},
|
||||
{
|
||||
"libraryName": "es6-promise",
|
||||
"typingsPackageName": "es6-promise",
|
||||
"sourceRepoURL": "https://github.com/jakearchibald/ES6-Promise",
|
||||
"asOfVersion": "3.3.0"
|
||||
},
|
||||
{
|
||||
"libraryName": "eventemitter2",
|
||||
"typingsPackageName": "eventemitter2",
|
||||
|
||||
@@ -1,193 +0,0 @@
|
||||
|
||||
|
||||
|
||||
var promiseString: Promise<string>,
|
||||
promiseStringArr: Promise<string[]>,
|
||||
arrayOfPromise: Promise<string>[],
|
||||
promiseNumber: Promise<number>,
|
||||
promiseAny: Promise<any>,
|
||||
thenable: Thenable<string>;
|
||||
|
||||
// constructor test
|
||||
var constructResult = new Promise<string>((resolve, reject) => {
|
||||
resolve('a string');
|
||||
});
|
||||
promiseString = constructResult;
|
||||
|
||||
|
||||
var constructResult1 = new Promise<string>((resolve:(promise: Thenable<string>) => void) => {
|
||||
resolve(Promise.resolve('a string'));
|
||||
});
|
||||
promiseString = constructResult1;
|
||||
|
||||
//resolve test
|
||||
var resolveResult = Promise.resolve('a string');
|
||||
promiseString = resolveResult;
|
||||
|
||||
var resolveResult1 = Promise.resolve(thenable);
|
||||
promiseString = resolveResult1;
|
||||
|
||||
//reject test
|
||||
var rejectResult = Promise.reject('there is an error');
|
||||
promiseAny = rejectResult;
|
||||
|
||||
//all test
|
||||
var allResult = Promise.all(arrayOfPromise);
|
||||
promiseStringArr = allResult;
|
||||
|
||||
//race test
|
||||
var raceResult = Promise.race(arrayOfPromise);
|
||||
promiseString = raceResult;
|
||||
|
||||
|
||||
//then test
|
||||
var thenWithPromiseResult = promiseString.then(word => Promise.resolve(word.length));
|
||||
promiseNumber = thenWithPromiseResult;
|
||||
|
||||
var thenWithPromiseResultAndPromiseReject = promiseString.then(word => Promise.resolve(word.length), error => Promise.resolve(10));
|
||||
promiseNumber = thenWithPromiseResultAndPromiseReject;
|
||||
|
||||
var thenWithPromiseResultAndSimpleReject = promiseString.then(word => Promise.resolve(word.length), error => 10);
|
||||
promiseNumber = thenWithPromiseResultAndSimpleReject;
|
||||
|
||||
var thenWithSimpleResult = promiseString.then(word => word.length);
|
||||
promiseNumber = thenWithSimpleResult;
|
||||
|
||||
var thenWithSimpleResultAndPromiseReject = promiseString.then(word => word.length, error => Promise.resolve(10));
|
||||
promiseNumber = thenWithSimpleResultAndPromiseReject;
|
||||
|
||||
var thenWithSimpleResultAndSimpleReject = promiseString.then(word => word.length, error => 10);
|
||||
promiseNumber = thenWithSimpleResultAndSimpleReject;
|
||||
|
||||
var thenWithUndefinedFullFillAndSimpleReject = promiseString.then(undefined, error => 10);
|
||||
promiseNumber = thenWithUndefinedFullFillAndSimpleReject;
|
||||
|
||||
var thenWithUndefinedFullFillAndPromiseReject = promiseString.then(undefined, error => Promise.resolve(10));
|
||||
promiseNumber = thenWithUndefinedFullFillAndPromiseReject;
|
||||
|
||||
var thenWithNoResultAndNoReject = promiseString.then<number>();
|
||||
promiseNumber = thenWithNoResultAndNoReject;
|
||||
|
||||
var catchAfterThen = promiseString.then().catch<number>();
|
||||
promiseNumber = catchAfterThen;
|
||||
|
||||
var voidPromise = new Promise<void>(function (resolve) { resolve(); });
|
||||
|
||||
//catch test
|
||||
var catchWithSimpleResult = promiseString.catch(error => 10);
|
||||
promiseNumber = catchWithSimpleResult;
|
||||
|
||||
var catchWithPromiseResult = promiseString.catch(error => Promise.resolve(10));
|
||||
promiseNumber = catchWithPromiseResult;
|
||||
|
||||
promiseString = promiseString.catch<string>(function (): string {
|
||||
throw new Error('Better error msg');
|
||||
});
|
||||
|
||||
//examples coming from http://www.html5rocks.com/en/tutorials/es6/promises/
|
||||
|
||||
function get(url: string) {
|
||||
// Return a new promise.
|
||||
return new Promise<string>(function(resolve, reject) {
|
||||
// Do the usual XHR stuff
|
||||
var req = new XMLHttpRequest();
|
||||
req.open('GET', url);
|
||||
|
||||
req.onload = function() {
|
||||
// This is called even on 404 etc
|
||||
// so check the status
|
||||
if (req.status == 200) {
|
||||
// Resolve the promise with the response text
|
||||
resolve(req.response);
|
||||
}
|
||||
else {
|
||||
// Otherwise reject with the status text
|
||||
// which will hopefully be a meaningful error
|
||||
reject(Error(req.statusText));
|
||||
}
|
||||
};
|
||||
|
||||
// Handle network errors
|
||||
req.onerror = function() {
|
||||
reject(Error("Network Error"));
|
||||
};
|
||||
|
||||
// Make the request
|
||||
req.send();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
function getJSON(url: string) {
|
||||
return get(url).then(JSON.parse);
|
||||
}
|
||||
|
||||
function addHtmlToPage(html: string) {
|
||||
|
||||
}
|
||||
|
||||
function addTextToPage(text: string) {
|
||||
|
||||
}
|
||||
|
||||
interface Story {
|
||||
heading: string;
|
||||
chapterUrls: string[]
|
||||
}
|
||||
|
||||
getJSON('story.json').then(function(story: Story) {
|
||||
addHtmlToPage(story.heading);
|
||||
|
||||
// Map our array of chapter urls to
|
||||
// an array of chapter json promises.
|
||||
// This makes sure they all download parallel.
|
||||
return story.chapterUrls.map(getJSON)
|
||||
.reduce(function(sequence, chapterPromise) {
|
||||
// Use reduce to chain the promises together,
|
||||
// adding content to the page for each chapter
|
||||
return sequence.then(function() {
|
||||
// Wait for everything in the sequence so far,
|
||||
// then wait for this chapter to arrive.
|
||||
return chapterPromise;
|
||||
}).then(function(chapter) {
|
||||
addHtmlToPage(chapter.html);
|
||||
});
|
||||
}, Promise.resolve());
|
||||
}).then(function() {
|
||||
addTextToPage("All done");
|
||||
}).catch(function(err) {
|
||||
// catch any error that happened along the way
|
||||
addTextToPage("Argh, broken: " + err.message);
|
||||
}).then(function() {
|
||||
(<HTMLElement>document.querySelector('.spinner')).style.display = 'none';
|
||||
});
|
||||
|
||||
interface T1 {
|
||||
__t1: string;
|
||||
}
|
||||
|
||||
interface T2 {
|
||||
__t2: string;
|
||||
}
|
||||
|
||||
interface T3 {
|
||||
__t3: string;
|
||||
}
|
||||
|
||||
function f1(): Promise<T1> {
|
||||
return Promise.resolve({ __t1: "foo_t1" });
|
||||
}
|
||||
|
||||
function f2(x: T1): T2 {
|
||||
return { __t2: x.__t1 + ":foo_21" };
|
||||
}
|
||||
|
||||
var x3 = f1()
|
||||
.then(f2, (e: Error) => {
|
||||
console.log("error 1");
|
||||
throw e;
|
||||
})
|
||||
.then((x: T2) => {
|
||||
return { __t3: x.__t2 + "bar" };
|
||||
});
|
||||
Vendored
-84
@@ -1,84 +0,0 @@
|
||||
// Type definitions for es6-promise
|
||||
// Project: https://github.com/jakearchibald/ES6-Promise
|
||||
// Definitions by: François de Campredon <https://github.com/fdecampredon>, vvakame <https://github.com/vvakame>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
interface Thenable<T> {
|
||||
then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
|
||||
then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => void): Thenable<U>;
|
||||
}
|
||||
|
||||
declare class Promise<T> implements Thenable<T> {
|
||||
/**
|
||||
* If you call resolve in the body of the callback passed to the constructor,
|
||||
* your promise is fulfilled with result object passed to resolve.
|
||||
* If you call reject your promise is rejected with the object passed to reject.
|
||||
* For consistency and debugging (eg stack traces), obj should be an instanceof Error.
|
||||
* Any errors thrown in the constructor callback will be implicitly passed to reject().
|
||||
*/
|
||||
constructor(callback: (resolve : (value?: T | Thenable<T>) => void, reject: (error?: any) => void) => void);
|
||||
|
||||
/**
|
||||
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
|
||||
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
|
||||
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
|
||||
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
|
||||
* If an error is thrown in the callback, the returned promise rejects with that error.
|
||||
*
|
||||
* @param onFulfilled called when/if "promise" resolves
|
||||
* @param onRejected called when/if "promise" rejects
|
||||
*/
|
||||
then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Promise<U>;
|
||||
then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => void): Promise<U>;
|
||||
|
||||
/**
|
||||
* Sugar for promise.then(undefined, onRejected)
|
||||
*
|
||||
* @param onRejected called when/if "promise" rejects
|
||||
*/
|
||||
catch<U>(onRejected?: (error: any) => U | Thenable<U>): Promise<U>;
|
||||
}
|
||||
|
||||
declare namespace Promise {
|
||||
/**
|
||||
* Make a new promise from the thenable.
|
||||
* A thenable is promise-like in as far as it has a "then" method.
|
||||
*/
|
||||
function resolve<T>(value?: T | Thenable<T>): Promise<T>;
|
||||
|
||||
/**
|
||||
* Make a promise that rejects to obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error
|
||||
*/
|
||||
function reject(error: any): Promise<any>;
|
||||
function reject<T>(error: T): Promise<T>;
|
||||
|
||||
/**
|
||||
* Make a promise that fulfills when every item in the array fulfills, and rejects if (and when) any item rejects.
|
||||
* the array passed to all can be a mixture of promise-like objects and other objects.
|
||||
* The fulfillment value is an array (in order) of fulfillment values. The rejection value is the first rejection value.
|
||||
*/
|
||||
function all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>, T10 | Thenable<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
|
||||
function all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
|
||||
function all<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
|
||||
function all<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>]): Promise<[T1, T2, T3, T4, T5, T6, T7]>;
|
||||
function all<T1, T2, T3, T4, T5, T6>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>]): Promise<[T1, T2, T3, T4, T5, T6]>;
|
||||
function all<T1, T2, T3, T4, T5>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>]): Promise<[T1, T2, T3, T4, T5]>;
|
||||
function all<T1, T2, T3, T4>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>]): Promise<[T1, T2, T3, T4]>;
|
||||
function all<T1, T2, T3>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>]): Promise<[T1, T2, T3]>;
|
||||
function all<T1, T2>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>]): Promise<[T1, T2]>;
|
||||
function all<T>(values: (T | Thenable<T>)[]): Promise<T[]>;
|
||||
|
||||
/**
|
||||
* Make a Promise that fulfills when any item fulfills, and rejects if any item rejects.
|
||||
*/
|
||||
function race<T>(promises: (T | Thenable<T>)[]): Promise<T>;
|
||||
}
|
||||
|
||||
declare module 'es6-promise' {
|
||||
var foo: typeof Promise; // Temp variable to reference Promise in local context
|
||||
namespace rsvp {
|
||||
export var Promise: typeof foo;
|
||||
export function polyfill(): void;
|
||||
}
|
||||
export = rsvp;
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es5",
|
||||
"dom"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"es6-promise-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json",
|
||||
"rules": {
|
||||
"adjacent-overload-signatures": false,
|
||||
"array-type": false,
|
||||
"arrow-return-shorthand": false,
|
||||
"ban-types": false,
|
||||
"callable-types": false,
|
||||
"comment-format": false,
|
||||
"dt-header": false,
|
||||
"eofline": false,
|
||||
"export-just-namespace": false,
|
||||
"import-spacing": false,
|
||||
"interface-name": false,
|
||||
"interface-over-type-literal": false,
|
||||
"jsdoc-format": false,
|
||||
"max-line-length": false,
|
||||
"member-access": false,
|
||||
"new-parens": false,
|
||||
"no-any-union": false,
|
||||
"no-boolean-literal-compare": false,
|
||||
"no-conditional-assignment": false,
|
||||
"no-consecutive-blank-lines": false,
|
||||
"no-construct": false,
|
||||
"no-declare-current-package": false,
|
||||
"no-duplicate-imports": false,
|
||||
"no-duplicate-variable": false,
|
||||
"no-empty-interface": false,
|
||||
"no-for-in-array": false,
|
||||
"no-inferrable-types": false,
|
||||
"no-internal-module": false,
|
||||
"no-irregular-whitespace": false,
|
||||
"no-mergeable-namespace": false,
|
||||
"no-misused-new": false,
|
||||
"no-namespace": false,
|
||||
"no-object-literal-type-assertion": false,
|
||||
"no-padding": false,
|
||||
"no-redundant-jsdoc": false,
|
||||
"no-redundant-jsdoc-2": false,
|
||||
"no-redundant-undefined": false,
|
||||
"no-reference-import": false,
|
||||
"no-relative-import-in-test": false,
|
||||
"no-self-import": false,
|
||||
"no-single-declare-module": false,
|
||||
"no-string-throw": false,
|
||||
"no-unnecessary-callback-wrapper": false,
|
||||
"no-unnecessary-class": false,
|
||||
"no-unnecessary-generics": false,
|
||||
"no-unnecessary-qualifier": false,
|
||||
"no-unnecessary-type-assertion": false,
|
||||
"no-useless-files": false,
|
||||
"no-var-keyword": false,
|
||||
"no-var-requires": false,
|
||||
"no-void-expression": false,
|
||||
"no-trailing-whitespace": false,
|
||||
"object-literal-key-quotes": false,
|
||||
"object-literal-shorthand": false,
|
||||
"one-line": false,
|
||||
"one-variable-per-declaration": false,
|
||||
"only-arrow-functions": false,
|
||||
"prefer-conditional-expression": false,
|
||||
"prefer-const": false,
|
||||
"prefer-declare-function": false,
|
||||
"prefer-for-of": false,
|
||||
"prefer-method-signature": false,
|
||||
"prefer-template": false,
|
||||
"radix": false,
|
||||
"semicolon": false,
|
||||
"space-before-function-paren": false,
|
||||
"space-within-parens": false,
|
||||
"strict-export-declare-modifiers": false,
|
||||
"trim-file": false,
|
||||
"triple-equals": false,
|
||||
"typedef-whitespace": false,
|
||||
"unified-signatures": false,
|
||||
"void-return": false,
|
||||
"whitespace": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user