- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个包含服务队列的数组,我需要一个接一个地运行这些服务,并将第一个函数的结果作为第二个函数的参数传递。服务可以是同步的和异步的。服务的结果可以是解决、拒绝或一些结果值。如果队列中的一个服务失败,则所有队列都应该失败,并且不运行队列中的下一个服务。
目前,我有一个 Promises 实现
let collection = [
{ name: 'calcOnServer', parameters: {} },
{ name: 'calc', parameters: {} }
];
return collection.reduce((currentFunction, nextFunction, index) => {
return currentFunction.then(() => {
let result = runFunction(nextFunction.name, nextFunction.parameters);
// runFunction is some method that can get class with name from first parameter and execute for it method 'run' with parameters from second parameter
if (result === undefined) {
result = Promise.resolve();
}
if (!result.then) {
if (Boolean(result)) {
result = Promise.resolve(result);
} else {
result = Promise.reject();
}
}
return result.then((result) => {
collection[index + 1].parameters = result;
});
});
}, Promise.resolve())
目前服务可以看起来像
class calcOnServer {
run({param1, param2}) {
return new Promise((resolve, reject) => {
// some async operation
.then(resolve, reject);
}
}
}
class calc {
run({param1, param2}) {
if (typeof param1 === 'number' && typeof param2 === 'number') {
return param1 + param2
} else {
return Promise.reject();
}
}
}
我需要用 RxJS/Observables 重写这个逻辑。
最佳答案
我不完全确定你的代码应该做什么,但一般来说,当你想迭代一个数组并对所有数组运行相同的异步操作时,你可以使用 mergeScan
运算符.它还接受一个可选的 concurrency
参数,告诉它你想运行多少个并行操作:
import { of, from } from 'rxjs';
import { map, mergeScan, delay } from 'rxjs/operators';
const collection = [1, 2, 3, 4, 5];
const source = from(collection).pipe(
mergeScan((acc, value) => {
console.log(acc, value);
return of(value).pipe( // Your async operation will be here
delay(1000),
map(response => `response-${response}`)
);
}, null, 1), // `1` will make `mergeScan` process items one by one
);
source.subscribe();
此示例将打印以下输出。请注意,每个日志都包含上一次调用的响应和当前值:
null 1
response-1 2
response-2 3
response-3 4
response-4 5
关于angular - rxjs/可观察到的 : Execute functions one by one and pass parameters of functions as result of previous function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57272935/
我有点困惑为什么在 RoutineRetrieved 函数中分配 ACTIVITYIMAGE 时使用 result.getInt(2) 并在分配 SLOT 时使用 result.getInt(3)..
我是android领域的新手,我想从响应json数组访问每个结果元素,但我无法做到这一点,我试图获取每个元素,但我只得到一个值“rohit1”是第一个元素。请帮助我。 我是 rohit parmar,
我只有从 sql 查询返回的一行 (count(*)),但在执行包时,它向我显示上述错误,并且包失败。 我将结果类型设置为“单行”,并将查询的输出(select count(*) as 'result
我正在尝试使用Diesel将简单的原始SQL转换为MySQL,如本示例所示: https://docs.diesel.rs/diesel/fn.sql_query.html let users = s
我正在尝试 Play 框架的第一个示例,但出现了此错误 在我的路线文件中: # API # ~~~~ GET /api/geotweets/index controllers.api.GeoTw
这段代码可以返回null吗? (this.Result == Result.OK) 此行(或类似行)是否可以返回除 true 或 false 之外的任何内容(例如 null)? 最佳答案 (this.
我有一个 SSIS 执行 SQL 任务。它返回一个完整的结果集(即一个表)。但是,当我执行包时出现以下错误。我已经正确地为返回的结果集命名。 [执行 SQL 任务] 错误:对于完整的结果集和 XML
最近我刚刚将 swift 2.3 项目转换为 3.2,alamofire 也得到了转换,我收到了很多问题,其中大部分都已解决,现在我被给定的两个问题所困扰 问题在 alamofire 的 Respon
我在 R 中收到以下错误消息: Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set", : Unable to r
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 去年关闭。
我正在使用一个简单的命令运行以下存储过程sp_MSforeachdb。我的问题是如何限制结果仅显示至少有 1 条记录满足命令的数据库: 这是我的存储过程: EXECUTE master.sys.sp_
我在单独的线程中运行一些代码: fn f1() -> Result { Err("err1".to_string()) } fn f2() -> Result { Err("err2"
我在这里尝试使用 AsyncTask 从 OWM API 显示 7 天的天气预报。 doInBackground(String...param) 方法也工作正常。我检查了 LOGCAT。 异步完成执行
我已经创建了mysql的索引和全文索引,后端存储引擎是MyISAM。 mysql> show index from play_movie; +------------+------------+---
我有一个表articles,它的结构如下: id, name, date, contents 我有一个表authors,它的结构如下: id, article_id, name, email 一篇文章
我很困惑我们是否应该创建单独的 API 来获取结果和结果计数,或者我们应该只根据结果 API 中的查询字符串来获取计数。 /api/results/ : Fetches all records /ap
我正在制作一个将两个数字相除的系统,如果第二个数字不存在,它将选择第一个数字。这是代码: let new_num: f32 = match num1/num2 { Ok(num) => n
这个问题在这里已经有了答案: Why am I getting "unused Result which must be used ... Result may be an Err variant,
我正在修改 the texture synth 中的示例之一项目: use texture_synthesis as ts; fn main() -> Result { //create a
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: ^ operator in java 我假设 c ^ d是一个类似“的幂”的计算,所以c = 5 , d = 2 ,
我是一名优秀的程序员,十分优秀!