gpt4 book ai didi

javascript - 如何将数组中的所有 onNext 值放在一起?

转载 作者:行者123 更新时间:2023-12-03 07:27:44 24 4
gpt4 key购买 nike

下面我有向两个 API 端点发出请求的代码。我正在寻找一种让 onNext() 函数仅触发一次的方法。现在,如果您运行下面的代码,您可以看到它 logs fire 1,然后是 fire 2。我只想让它触发一次,并将两者的内容都放在一个数组中。

import axios from 'axios'
import Rx from 'rx'
import assert from 'assert'

describe('rx axios', () => {
it('should make http request to api', (done) => {
let array = ['http://jsonplaceholder.typicode.com/users/1', 'http://jsonplaceholder.typicode.com/users/2']

function httpGet(url) {
return axios.get(url)
}

let source = Rx.Observable
.fromArray(array)
.concatMap(httpGet)

let count = 0

let subscription = source.subscribe(
function (responses) {
console.log(`fire ${count}`)
count++
// assert.equal(responses[0].data.name, 'Leanne Graham')
// assert.equal(responses[1].data.name, 'Ervin Howell')
},
function (err) {
done(err)
},
function () {
done()
})

})
})

最佳答案

您应该能够使用toArray 。例如:

 let source = Rx.Observable
.fromArray(array)
.concatMap(httpGet)
.toArray()

我相信 toArray 也已进入 RxJs v5,但如果您正在使用该版本,请不要相信我的话并检查它。我正在使用 v4。

关于javascript - 如何将数组中的所有 onNext 值放在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35931822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com