简体   繁体   中英

How to force post-processing to wait until all fetch processes finish in node.js?

I am collecting multiple data using fetch and then use them in multiple calculations.

var data_dict = []; // objects with ids

var fetch_data = function() {

    let data_history = [];
    data_dict.forEach(function (item, index) {
        let id = item.id;

        fetch(// with id)
            .then(res => res.json()) 
            .then(data=>{// fix data})
            .then(fixed_data=> data_history.push(fixed_data))
            .catch(err => console.log(err))
    });

    return data_history;
}

var use_fetched_data = function () {
    let results = [];

    // use data_history in calculating results

    return results;
}

var use_fetched_data2 = function () {
    let results = [];

    // use data_history and obtained results in calculating another results

    return results;
}

// get data_history2
var data_history2 = fetch_data();

// use data_history2 to find results1
var results1 = use_fetched_data ();

// use data_history2 and results1 to find results2
var results2 = use_fetched_data2 ();

I tried to use async & await to force waiting until all data are fetched, but results still get calculated before fetch is completed.

var force_wait = async function() {

    // get data_history2
    var data_history2 = await fetch_data();

    // use data_history2 to find results1
    var results1 = use_fetched_data ();

    // use data_history2 and results1 to find results2
    var results2 = use_fetched_data2 ();
}

force_wait();

How to do this properly?

thanks

The problem is that your fetch_data function doesn't wait for the requests:

const fetch_data = async function () {
    const data_history = [];

    requests = data_dict.map(({ id }, index) => {
        return fetch('https://randomuser.me/api/')
            .then(res => res.json())
            .then(data => data.results[0])
            .then(fixed_data => data_history.push(fixed_data))
    });
    try {
      await Promise.all(requests)
    } catch (err) {
      console.error(err)
    }
    return data_history
}

After this change, your force_wait should work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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