从承诺链到 Rx.js
From promise chain to Rx.js
我有一个如下所示的承诺链,其中包含三个承诺。我已经研究 Rx.js 一段时间了,在将这些 promises 转换为 observables 并将这些信息链接到不同的函数中时,我不知道从哪里开始。如果可能的话,我真的很感激一些指导/知识。
export function pushCustomers (mongo, shopify) {
return getDocsWhereRequest(mongo, 'shopify_customers').map(customer => {
return createCustomer (shopify, customer.shopifyRequest).then(shopifyResponse => {
return updateCollection(mongo, 'shopify_customers', {email: customer.email}, {shopifyResponse})
}).catch(err => {
if (!_.get(err, 'response.body.errors')) throw err
let shopifyResponseError = JSON.stringify(err.response.body.errors)
return updateCollection(mongo, 'shopify_customers', {email: customer.email}, {shopifyResponseError})
})
})
}
正如 Ben 在他的评论中提到的,RxJs 中的一些运算符接受 promises 并将它们隐式转换为可观察对象。关于承诺链,以下是两个资源,可以帮助您加深理解。第一个涉及链接,第二个也涉及链接但侧重于错误管理:
- Observable - converting 2 promises into an observable
我有一个如下所示的承诺链,其中包含三个承诺。我已经研究 Rx.js 一段时间了,在将这些 promises 转换为 observables 并将这些信息链接到不同的函数中时,我不知道从哪里开始。如果可能的话,我真的很感激一些指导/知识。
export function pushCustomers (mongo, shopify) {
return getDocsWhereRequest(mongo, 'shopify_customers').map(customer => {
return createCustomer (shopify, customer.shopifyRequest).then(shopifyResponse => {
return updateCollection(mongo, 'shopify_customers', {email: customer.email}, {shopifyResponse})
}).catch(err => {
if (!_.get(err, 'response.body.errors')) throw err
let shopifyResponseError = JSON.stringify(err.response.body.errors)
return updateCollection(mongo, 'shopify_customers', {email: customer.email}, {shopifyResponseError})
})
})
}
正如 Ben 在他的评论中提到的,RxJs 中的一些运算符接受 promises 并将它们隐式转换为可观察对象。关于承诺链,以下是两个资源,可以帮助您加深理解。第一个涉及链接,第二个也涉及链接但侧重于错误管理:
- Observable - converting 2 promises into an observable