导出 chain promise 的 return 值

Export the return value of chain promise

如何导出多重承诺的 returned 值。 我需要将两个数据从第一个 API 导出到第二个 API 数据。这意味着将第一个数据插入数组,然后将第二个数据插入 return 该数组并导出以从另一个 .js 文件使用。

我试图搜索如何 ​​returned then() 方法中的值。但是当我将导入的文件调用到另一个 .js 文件时,它会记录 [Promise, Promise, Promise...]

API.js

function getAllData(url) {
    return axios.get(url, {
        headers: {
            "Accept": "application/json; odata=verbose"
        }
    }).then(response => response.data.d.results);
}

function getAllDataVHistory(data) {

    return data.map(i => axios.get(`urltwo?getID${i.Id}`).then(response => {
        return response.data;
    }));
}

export const final = () =>
    getAllData(`urlone`)
    .then(data => getAllDataVHistory(data));

Display.js

import {
   final
} from "./API.js";

final().then(en => {
    // return new Promise((resolve, reject) => {
    //   if (true) {
    //    return resolve(console.log(en));
    //   } else {
    //    return reject("promise failed");
    //   }
    // });
   console.log(en);
});

Console.log 结果

(3) [Promise, Promise, Promise]
return data.map(i => axios.get(`urltwo`)

这将 return 一组 承诺

由于您正在解决一个承诺,因此您将获得该数组(因此您在日志中看到的内容)。

您需要return一个单一承诺,这样它才会被采纳。

Use Promise.all(array_of_promises) 创建一个承诺,当数组中的所有承诺都已解决时,该承诺将解决。