简体   繁体   English

扩展WinJS Promise对象

[英]Extending WinJS Promise object

I wanted an easy way to add pauses into some stuff that I chain together with promises. 我想要一种简单的方法来将停顿添加到我与承诺链接在一起的某些内容中。

I thought it would be nice to include a "wait" method so I could write stuff like: 我认为最好包含一个“ wait”方法,这样我就可以编写如下内容:

var promise = new WinJS.Promise(function(complete){
    whatever()
}).wait(1000).then(function(){
    whatever2();
}).wait(500).done(function(){
    alldone();
}

So to do this I added a wait method to the Promise class like so: 为此,我向Promise类添加了一个wait方法,如下所示:

if (WinJS.Promise.prototype.wait == null) {
    WinJS.Promise.prototype.wait = function (milliseconds) {
        var promise = new WinJS.Promise(function (complete) {
            setTimeout(complete, milliseconds);
        });
        return promise;
    }
}

It seemed to be working, but I noticed that if I use a "then", the object I get back from it, while the documentation says is a WinJS.Promise, won't have a wait function. 它似乎正在工作,但是我注意到,如果我使用“ then”,则从它取回的对象,而文档中说的是WinJS.Promise,将没有等待功能。 The promises I create all DO have the wait function, but calling .then() on a promise will cause the subsequent .wait to fail, so... 我创建的所有DO的Promise具有wait函数,但是对Promise调用.then()会导致后续的.wait失败,所以...

promise.wait(300).then().done();

is no problem but: 没问题,但是:

promise.then().wait(300).done();

will error out saying that there is no wait method on the Promise returned from then(). 将错误地指出,从then()返回的Promise上没有等待方法。

Can anyone explain what I'm doing wrong? 谁能解释我在做什么错?

There are two reasons why your code doesn't work. 您的代码无法正常工作的原因有两个。

The first is that Microsoft has used their own approach to creating object instances, which they do through the WinJS.Class namespace - this means that the prototype you are altering with the addition of your wait function doesn't ever get applied to the WinJS.Promise objects you are consuming in your code. 首先是Microsoft使用了自己的方法来创建对象实例,这些方法是通过WinJS.Class命名空间来完成的-这意味着您添加wait功能而更改的原型永远不会应用于WinJS.Promise您在代码中使用的对象。

The second problem is that you are targeting the wrong object - the WinJS.Promise.then method calls WinJS.Promise.as which returns a CompletePromise object - so even if you could make your wait function stick, it would be in the wrong place. 第二个问题是您定位到错误的对象WinJS.Promise.then方法调用WinJS.Promise.as ,它返回CompletePromise对象-因此,即使您可以使wait函数WinJS.Promise.as在正确的位置,它也将放在错误的位置。 The CompletePromise definition is not in the public scope, so you'd have to do a lot of hacking to be able make the change you want. CompletePromise定义不在公共范围内,因此您必须进行大量修改才能进行所需的更改。

There is a solution, but you have to use the WinJS.Promise.timeout method. 一个解决方案,但你必须使用WinJS.Promise.timeout方法。 You can't use this inline, which means that to get the effect you want, you will need some slightly awkward code, as follows; 您不能使用此内联,这意味着要获得所需的效果,您将需要一些稍显尴尬的代码,如下所示;

var promise = new WinJS.Promise(function (complete) {
    whatever();
    complete();
}).then(function () {
    return WinJS.Promise.timeout(1000);
}).then(whatever2).then(function() {
    return WinJS.Promise.timeout(500);
}).then(alldone);

This not a direct answer to your question. 这不是您问题的直接答案。 Adding a wait() method to Promise 's prototype should indeed work, unless then() returns an object that looks like a Promise , quacks like a Promise , but is not actually a Promise . 添加wait()方法,以Promise的原型的确应该工作,除非then()返回一个对象,看起来像一个Promise ,叫声也像Promise ,但实际上不是一个Promise

That said, you do not have to implement a wait() method in the first place, because Promise already exposes a timeout() method that does the same thing (and more, actually). 就是说,您不必首先实现wait()方法,因为Promise已经公开了执行相同操作的timeout()方法(实际上,还有更多方法)。 You're looking for its single-argument form: 您正在寻找其单参数形式:

var promise = new WinJS.Promise(function(complete) {
    whatever();
}).timeout(1000).then(function() {
    whatever2();
}).timeout(500).done(function() {
    alldone();
});

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

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