简体   繁体   English

2 Promise中的SetTimeOut方法不起作用

[英]2 SetTimeOut Method inside Promise not working

Couldn't find a solution for the same hence reaching out to you guys for help. 找不到相同的解决方案,因此向你们寻求帮助。

I have 2 JS method that needs to be called a specific time interval continuously so to achieve this i used SetTimeout as shown below but it does not seem to work and only second setTimeout is called . 我有2个JS方法,需要连续调用一个特定的时间间隔,以便实现此目的,我使用了SetTimeout,如下所示,但它似乎不起作用,仅调用了第二个setTimeout。 Ideally i expected that both methods would be called after the mentioned time. 理想情况下,我希望在提到的时间之后将调用这两种方法。

My code:- 我的代码:-

var statusPromise = new Promise(function(resolve, reject) {
    setTimeout(function() {
        plugin.dcsHelper.getMeterStatus(function(data) {
            plugin.dcsHelper.log("INFO", "Status: " + JSON.stringify(data));
            if (data && data.status) {
                resolve(data.status);
            }
        }, function(err) {
            plugin.dcsHelper.log("ERROR", "Error while polling: " + JSON.stringify(err));
        });
    }, 1000);
});

var progressPromise = new Promise(function(resolve, reject) {
    setTimeout(function() {
        plugin.dcsHelper.getProgress2(function(data) {
            plugin.dcsHelper.log("INFO", "Value: " + JSON.stringify(data));
            if (data && data.val) {
                resolve(data.val);
            }
        }, function(err) {
            plugin.dcsHelper.log("ERROR", "Error while polling: " + JSON.stringify(err));
        });
    }, 5000);
});

Promise.all([statusPromise, progressPromise]).then(function(values) {
    console.log(values);
});

Here the method inside progressPromise is only getting called and the method inside statusPromise is not getting called. 在这里,仅会调用progressPromise内部的方法,而不会调用statusPromise内部的方法。

Any help will be much appreciated.!! 任何帮助都感激不尽。!!

Actually the above code is completely right and promise works as expected . 实际上,以上代码是完全正确的,promise可以按预期工作。 The problem was with the Plugin java code which i was using. 问题出在我正在使用的Plugin Java代码中。

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

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