简体   繁体   English

从多个 api 查询按顺序返回结果

[英]Returning results in their order from multiple api queries

I want to make multiple API calls from an array of values.我想从一组值中进行多次 API 调用。 I would like to resolve the first call, then the second, third, and so on in the order in which the API request receives the data.我想按照 API 请求接收数据的顺序来解决第一个调用,然后是第二个、第三个,依此类推。 everything I've tried gets me to the point of logging the final data in the console or resolving the data but not necessarily returning the array of data in the format with which the requests were made.我所做的一切都让我能够在控制台中记录最终数据或解析数据,但不一定以发出请求的格式返回数据数组。 Below is one of the solutions I was recently playing with.以下是我最近使用的解决方案之一。 It definitely does not work (and there are a lot of issues with it) but I do see the unresolved data in the console.它肯定不起作用(并且存在很多问题),但我确实在控制台中看到了未解决的数据。 The console.logs just show the points where some sort of accurate data is returned as an unresolved promise console.logs仅显示某些准确数据作为未解析的 promise 返回的点

Function Function

const sendTrackingData =  (storeKey, deliveries) => {
    const trackingUrl = deliveries.map(async (delivery) => {
        const urlData = []

        delivery.parcels.map(async (parcel) => {
            const {trackingData} = parcel;
            const carrier = trackingData.carrier.toLowerCase();
            const trackingCode = trackingData.trackingId;
            urlData.push(await getTrackingUrl(storeKey, carrier, trackingCode))
        })
        console.log(urlData)
        return urlData
    })
    return Promise.all(trackingUrl)
};

It is called here for now in a use effectuse effect中暂时称为这里

    const test = await sendTrackingData(storeKey, deliveries)
    console.log(test,'the test url data =========>')

API call API 调用

export const getTrackingUrl =  (storeKey, carrier, trackingCode) => {
    return axios.post(`random-url-address/returnedData`, {
        storeKey,
        carrier,
        trackingCode,
    });
};


You need to call Promise.all on the main asynchronous requests - the getTrackingUrl - for the order to be preserved:您需要在主要异步请求getTrackingUrl )上调用Promise.all以保留订单:

const sendTrackingData = (storeKey, deliveries) => (
    Promise.all(deliveries.map((delivery) => (
        Promise.all(delivery.parcels.map((parcel) => {
            const { trackingData } = parcel;
            const carrier = trackingData.carrier.toLowerCase();
            const trackingCode = trackingData.trackingId;
            return getTrackingUrl(storeKey, carrier, trackingCode);
        }))
    )))
);

When you want to preserve order, .push after an asynchronous request is almost never the right approach.当您想保持顺序时,异步请求后的.push几乎不是正确的方法。

you can try你可以试试

 delivery.parcels.map(async (parcel, index) => { const {trackingData} = parcel; const carrier = trackingData.carrier.toLowerCase(); const trackingCode = trackingData.trackingId; urlData.splice(index, 0, await getTrackingUrl(storeKey, carrier, trackingCode)); }) console.log(urlData) return urlData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM