【发布时间】:2014-08-19 13:43:38
【问题描述】:
在 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();
});
它有效,但对“全局”的引用很难看。
注意。我没有使用definition file from DefinitelyTyped。
【问题讨论】:
标签: node.js typescript promise definitelytyped rsvp-promise