Node.js で、TypeScript で promise を使用する適切な方法は何ですか?
現在、定義ファイル「rsvp.d.ts」を使用しています。
interface Thenable {
then(cb1: Function, cb2?: Function): Thenable;
}
declare module rsvp {
class Promise implements Thenable {
static cast(p: Promise): Promise;
static cast(object?): Promise;
static resolve(t: Thenable): Promise;
static resolve(obj?): Promise;
static reject(error?: any): Promise;
static all(p: Promise[]): Promise;
static race(p: Promise[]): Promise;
constructor(cb: Function);
then(cb1: Function, cb2?: Function): Thenable;
catch(onReject?: (error: any) => Thenable): Promise;
catch(onReject?: Function): Promise;
}
}
…そして私の「.ts」ファイルでは:
/// <reference path='../node.d.ts' />
/// <reference path='rsvp.d.ts' />
global['rsvp'] = require('es6-promise');
var Promise = rsvp.Promise;
var p: Thenable = new Promise(function (resolve) {
console.log('test');
resolve();
});
それは機能しますが、「グローバル」への参照は醜いです。
注意。FixedTyped の定義ファイルを使用できませんでした。