等待并且不执行功能,直到指示灯设置为绿色 [英] Wait and don't execute a function until a light is set to green
问题描述
我了解我们可以使用.then
来确保异步调用的顺序:
I understand that we could use .then
to make sure the order of asynchronous calls:
return doTask1()
.then(function () {
return doTask2()
})
但是有时有时会很方便,并且可以说:在灯光设置为GREEN之前,请等待并不要执行task2 ;指示灯是最初设置为RED的变量,可以通过task1或其他功能设置为GREEN.
But sometimes it will be convenient to have a light and to be able to say: wait and don't execute task2 until a light is set to GREEN; the light is a variable initially set to RED, and can be set to GREEN by task1 or other functions.
有人知道是否有可能做到这一点?
Does anyone know if it is possible to accomplish this?
我认为能够表达这一点特别有用,当我们需要结束多个任务以设置浅绿色时,而我们不知道/不注意顺序这些任务. .then
不能轻易做到这一点,因为我们不知道这些任务的顺序.
Edit 1: I think being able to express this is particularly useful when we need several tasks to be ended to set the light green, and we don't know/mind the order of these tasks. .then
cannot do this easily, because we don't know the order of these tasks.
关于my light
,我问了一个更具体的问题.一句话,就是我们正在等待的另一个应用程序B通过postMessage
发送的消息.目前,我已经编写了下面的代码(在resolve
中),该代码或多或少地起作用(我没有尝试过仅使一个功能与它们的共同部分起作用).
Edit 2: Regarding my light
, I had asked a more specific question. In one word, it is the message another application B sends by postMessage
that we are waiting for. At the moment, I have written the following code (which is in a resolve
), which works more or less (I have not tried if making only ONE function with their common part will work).
task1: ['codeService', '$window', '$q', function (codeService, $window, $q) {
return codeService.task1().then(function () { // task1 sends a request to another application B by postMessage
var deferred = $q.defer();
$window.addEventListener("message", function (event) {
if (event.data.req === "task1ReturnFromB") deferred.resolve(event.data)
}, { once: true });
return deferred.promise
})
}],
task2: ['codeService', 'task1', '$window', '$q', function(codeService, task1, $window, $q) {
return codeService.task2().then(function () { // task2 sends a request to Application B by postMessage
var deferred = $q.defer();
$window.addEventListener("message", function (event) {
if (event.data.req === "task2ReturnFromB") deferred.resolve(event.data)
}, { once: true });
return deferred.promise
})
}]
因此,在这种特定情况下,应用程序B发送的postMessage
触发事件.在更一般的情况下,我想我们可能可以通过在一个应用程序中通过 dispatchEvent 来触发事件?
So in this specific case, postMessage
sent by Application B triggers the event. In a more general case, I guess we could probably trigger an event by eg, dispatchEvent, in one application?
推荐答案
您还没有告诉我们有关您的light的API的信息,所以我只能猜测它是什么样子.假设它是事件驱动的模型,则可以将其转换为Promise,然后执行以下操作:
You haven't told us anything about the API for your light so I can only guess what it's like. Presuming it's an event driven model, you can convert it to a promise, and do this:
function waitForGreenLight() {
return new Promise(function (resolve) {
$window.addEventListener("message", function (event) {
if (event.data.req === "task2ReturnFromB") {
resolve(event.data)
}
}, { once: true });
});
}
return doTask1()
.then(waitForGreenLight)
.then(doTask2);
如果您的灯不提供事件,则可以使用waitForGreenLight
定期对其进行轮询,直到其变绿为止.使用waitForGreenLight
的代码将保持不变.
If your light doesn't provide events, you could have a waitForGreenLight
that periodically polls it until it's green. The code to use waitForGreenLight
would remain the same.
function waitForGreenLight() {
return new Promise(function (resolve) {
var handle = setInterval(function () {
if (myLight.color === 'green') {
resolve();
clearInterval(handle);
}
}, 100);
});
}
这篇关于等待并且不执行功能,直到指示灯设置为绿色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!