简体   繁体   English

Type方法的Typescript序列

[英]Typescript sequence of a Class methods

I got a typescript class with an attribute that contain some connection data that are the result of an ajax call, here's a snippet: 我有一个带有属性的打字稿类,其中包含一些由ajax调用产生的连接数据,这是一个代码段:

class User {
    // ... other stuff (constructor, attributes, methods)
    static data:{id:number; token:string} = {id: 0, token: ""};
    connect() {
        // Ajax Call
        .done(function(e){
            User.data.id = e.id;
            User.data.token = e.token;
        })
    }
    request() {
        if(User.data.id == 0)
            setTimeout(() => { 
                this.request();
            }, 500);
        else return  '?id=' + User.data.id + '&token=' + User.data.token;
    }
}

I tried to use connect() and subsequently request() but sometimes the request() function starts before ajax's answer. 我尝试使用connect()和随后的request()但有时request()函数在ajax的答案之前启动。 Now I'm trying to write the request() function with some waiting time and a sort of recursion. 现在,我正在尝试编写带有一些等待时间和某种递归的request()函数。 Unfortunately it doesn't work.. My goal is to call the request() function and obtain the string only when "id" and "token" are ready (not 0 and empty string). 不幸的是,它不起作用。我的目标是仅在“ id”和“ token”准备就绪时才调用request()函数并获取字符串(而不是0和空字符串)。 Any suggestion would be appreciated! 任何建议,将不胜感激!

PS: I can't put the request() function inside the ajax callback: the two functions should be separated PS:我不能将request()函数放在ajax回调中:这两个函数应该分开

Once you are in promise land you need to stay there (or use a callback): 一旦进入承诺土地,您就需要呆在那里(或使用回调):

class User {
    // ... other stuff (constructor, attributes, methods)
    static data: { id: number; token: string } = { id: 0, token: "" };

    private connectedPromise;
    connect() {
        // Ajax Call
        this.connectedPromise = something.done( function ( e ) {
            User.data.id = e.id;
            User.data.token = e.token;
        })
    }
    request() {
        this.connectedPromise.then( () => {
            if ( User.data.id == 0 )
                setTimeout( () => {
                    this.request();
                }, 500 );
            else return '?id=' + User.data.id + '&token=' + User.data.token;
        })
    }
}

Store the result of connect in a var on only execute request if connect has resolved. 如果连接已解决,则仅在执行请求时将连接结果存储在var中。

PS the request function is bad (I haven't cleaned it up but removed the return) in that it is maybe async ie it either returns a value or does some async work. PS的请求功能是坏的(我还没有清理,但删除了回报),因为它可能是异步的,即它要么返回一个值或做一些异步工作。 Better to be consistent and always be async. 最好保持一致,并始终保持异步。

The calling code should be combining the promises with then . 调用代码应该将promise与then结合在一起。 connect() and request() should just return their promises. connect()request()应该只返回其诺言。 eg they "promise to return done when connected" and "promise to return the requested data". 例如,它们“承诺在连接后返回完成”和“承诺返回所请求的数据”。 In the case of request , it does not need a deferred object or promise as it simply returns a value immediately. 对于request ,它不需要deferred对象或Promise,因为它只是立即返回一个值。

JSFiddle example: http://jsfiddle.net/TrueBlueAussie/qcjrLv4k/3/ JSFiddle示例: http : //jsfiddle.net/TrueBlueAussie/qcjrLv4k/3/

// Return a fake connection after 5 seconds
var connect = function (){
    var def = $.Deferred();
    setTimeout(function(){
        def.resolve();
    }, 5000);
    return def.promise();
}

var request = function(){
    return "?data=d";
}

and use like this: 并像这样使用:

connect().then(request).done(function(data){
    alert(data);
});

So in your case, just return the ajax call result as a promise from connect() : 因此,在您的情况下,只需从connect()返回ajax调用结果作为promise:

   connect() {
        return $.ajax({[snipped]})
        .done(function(e){
            User.data.id = e.id;
            User.data.token = e.token;
        });
    }

and request simply returns the string: 并且request仅返回字符串:

request() {
    return '?id=' + User.data.id + '&token=' + User.data.token;
}

Another way to do it is to save the connect promise: 另一种方法是保存connect承诺:

var promise = connect();

and use it whenever you want to get the request data: 并在需要获取request数据时使用它:

promise.then(request).done(function(data){
    alert(data);
});

A better way , if your request is dependent on a "connection", is to pass the connect promise to the request method as a required parameter: 如果您的request依赖于“连接”, 则更好的方法是将connect request作为必需参数传递给request方法:

request(connectionPromise){
    return connectionPromise.then(function(){
         return '?id=' + User.data.id + '&token=' + User.data.token;
    });
};

and call with: 并致电:

request(connect()).done(function(data){
   alert(data);
});

Example using this approach here: http://jsfiddle.net/TrueBlueAussie/qcjrLv4k/4/ 在此使用此方法的示例: http : //jsfiddle.net/TrueBlueAussie/qcjrLv4k/4/

Final example (based on commented desire to reuse connection). 最终示例 (基于对重用连接的评论愿望)。 Combine the previous answers like this: 结合这样的先前答案:

1) Save the connection as a property of the object. 1)将连接另存为对象的属性。

// Start the connection process and save the promise for re-use
var connectionPromise = connect();

2) Use the connection as a parameter to the requests (so they do not need to have knowledge of the external variable): 2)使用连接作为请求的参数(因此它们不需要了解外部变量):

// Make a request, passing the connection promise
request(connectionPromise).done(function(data){
   alert(data);
});

3) The request method does not change from the previous example: 3)请求方法与前面的示例相同:

request(connectionPromise){
    return connectionPromise.then(function(){
         return '?id=' + User.data.id + '&token=' + User.data.token;
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM