在TypeScript中,如何在Node.js上使用带有RSVP实现的Promises

时间:2014-06-28 16:39:42

标签: node.js typescript promise definitelytyped rsvp-promise

在Node.js上,使用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();
});

它有效,但对“全球”的提及很难看。

NB。我没有使用definition file from DefinitelyTyped

1 个答案:

答案 0 :(得分:1)

正确修复

刚刚更新了定义以支持正确的方式:https://github.com/borisyankov/DefinitelyTyped/pull/2430

现在可以使用相同的defs(https://github.com/borisyankov/DefinitelyTyped/tree/master/es6-promises):

import rsvp = require('es6-promises');
var Promise = rsvp.Promise;

只是对未来的建议

  

它有效,但引用了&#34;全球&#34;很难看

你可以这样做:

var rsvp = require('es6-promise');
var Promise = rsvp.Promise;