vendredi 20 février 2015

This does not seem to be returning the correct object (Typescript)

I have created a small class, which wrappers around a generic callback function signature, and returns a promise.


however then it calls the handle menthod the _resolve is undefined.



/// <reference path="typings/es6-promise/es6-promise.d.ts" />
import rsvp = require('es6-promise');
var Promise = rsvp.Promise;

/** * wrapper a callback style call into a promise */ class
Promiseify<T> {
private _promise : Promise<T>;
private _resolve : (result?: T) => void;
private _reject : (error: any) => void;

/**
* create wrapper, example: new Promiseify(call).promise
* @param call example (handle) => yourDb.update(document, handle)
*/
constructor(call:(handle:(error: any, result: T) => void) => void){

this._promise = new Promise<T>((resolve, reject) =>{
this._resolve = resolve;
this._reject = reject;
call(this.handle);
});

}

handle(error: any, result: T) {
if(error != null) this._reject(error);
else this._resolve(result);
}

/**
* get the es6 promise which can then be passed to external callers
* @returns {Promise<T>} the es6 promise which this class creates.
*/
get promise() : Promise<T>{
return this._promise;
}
}

//the test rig
new Promiseify<string>((handle)=> {
handle(null, 'hello world');
}).promise.then((greeting: string)=> {
console.log(greeting);
});


am i missing something, btw the JS under the covers looks ok too


also if it helps I am running this on Node.JS, inside Webstorm 9.03


Aucun commentaire:

Enregistrer un commentaire