0
我使用Node.js的Node.js的 - 有回調反覆
我試圖做的是調用時觸發的承諾和它的回調函數返回一個鏈中的功能開發的JavaScript的API函數運行一個產品信息數組。
我想要做的就是根據需要多次運行此函數以形成JSON格式的產品列表。
但是這個產品列表需要在它被髮回給用戶之前創建,這是我掙扎的地方。
這是我具備的功能:
exports.findProduct = (ingredient, store, callback) => {
products.getProductData(ingredient, store)
.then(data => products.getProducts(data))
.then(productList => products.filterProductData(productList))
.then(selectedProduct => callback(null,selectedProduct))//get individual products and add to list
.catch(err => callback(err))
}
我這樣稱呼它
products.findProduct(ingredient, 'Waitrose', (err, data) => {
if (err) {
//res.send(status.badRequest, {error: err.message})
} else {
res.send(data)
res.end()
}
})
的res.send(data)
從回調返回到服務器,但如果我要運行的發送數據這個函數再次和更新一個數組,然後才發送數組到服務器?
任何技巧的幫助將非常感激。
非常感謝你的回答,只有一個小問題。在if語句中,'allProducts.push(newProducts)'將與第一個'allProducts.push(newProducts)'相同的元素推入。這導致類似'[product1,[product2,[product3]]]]'任何想法如何避免這個?乾杯 – al3
我編輯了我的答案以使用spread(...)運算符,以便newProducts中的項目被添加到allProducts,而不是數組本身。 –
經過反思,我還想知道是否有更簡單的方法來做到這一點,所以添加了一種不同的方法,可能適用於您的情況,也可能不適合您的情況。 –