等待并且在灯设置为绿色之前不执行功能 [英] Wait and don't execute a function until a light is set to green

查看:19
本文介绍了等待并且在灯设置为绿色之前不执行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以使用 .then 来确保异步调用的顺序:

I understand that we could use .then to make sure the order of asynchronous calls:

return doTask1()
    .then(function () {
        return doTask2()
    })

但有时有一个灯会很方便,并且可以说:等待并且不要执行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.

编辑 2:关于 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?

推荐答案

你没有告诉我们任何关于你的灯的 API,所以我只能猜测它是什么样的.假设它是一个事件驱动模型,您可以将其转换为承诺,然后执行以下操作:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆