在 Node.js 上,将 Promise 与 TypeScript 一起使用的正确方法是什么?
目前我使用定义文件“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();
});
它有效,但对“全局”的引用是丑陋的。
注意。我未能使用来自definitelyTyped 的定义文件。