我在看promises package,但我不知道如何让promises做任何事情。所有可用的阻塞机制(如promise_all)都返回一个promise,而且似乎没有明显的方法可以让promise在第一时间执行。例如,给定以下代码片段:
library(promises)
p <- promise(~ {
print("executing promise")
}) %>% then(~ {
print("promise is done")
})
print("we are here")
done <- FALSE
all_promises <- promise_all(p) %>% then(~ {
print("all promises done")
done <<- TRUE
})
# output:
# [1] "executing promise"
# [1] "we are here"我如何实际调用promise链?
奇怪的是,如果我将第一个promise更改为future_promise并添加一个run循环,如下所示
while(!done) {
later::run_now()
Sys.sleep(0.01)
}promise链正确执行。然而,这并不适用于常规的承诺。
我遗漏了什么?看起来系统缺少一个执行者,但是我从哪里得到一个执行者呢?我在包本身中没有看到任何API,也没有用户可见的API来查询我能看到的承诺。
发布于 2021-07-01 21:20:59
事实证明我错误地使用了API。promise表达式应该调用延续回调。我错过了这个细节。所以这是可行的:
library(promises)
p <- promise(~ {
print("executing promise")
resolve(1)
}) %>% then(~ {
print("promise is done")
})
print("we are here")
done <- FALSE
all_promises <- promise_all(p) %>% then(~ {
print("all promises done")
done <<- TRUE
})
while(!done) {
later::run_now()
Sys.sleep(0.1)
}https://stackoverflow.com/questions/68210788
复制相似问题