【问题标题】:How to call methods one after one?如何一个接一个地调用方法?
【发布时间】:2020-12-08 11:39:25
【问题描述】:

有一个函数接受三个参数:

function getAddressStructure(country: number, city: number, region: number) {

}

我需要创建一个 promise,为每个变量返回数据,结果返回一个公共对象 {countries: [], cities: [], regions: []}

如何使用 promise(s) 做到这一点?

let promise = new Promise((resolve, reject) => resolve());

【问题讨论】:

  • 为什么要使用 Promise?更准确地描述您的问题。
  • 因为我需要在获得上一个结果时执行下一个请求,所以加载国家 -> 加载城市(带有国家结果 id)-> 加载区域(带有城市结果 id)
  • 您有三个不同的 API 端点吗?获取每个变量的数据?
  • 是的,三个不同的 API

标签: javascript typescript


【解决方案1】:

你想要实现的,你可以使用回调函数来实现这些行为,而无需使用promise。

async function getAddressStructure(country(args, getCities,getregioR)
{
    var countries = await getCountries():
    var cities= await getCities ():
    var regions = await  getRegion ();

    return { countries : countries, cities: cities, regions: regions}
}

async function getCountries ();
async function getCities() {}
async function getRegion () {}

【讨论】:

    【解决方案2】:

    如果您有三个不依赖于彼此的不同端点,那么您可以使用这种方法:

    export async function handleResponse(response) {
      if (response.ok) return response.json();
      if (response.status === 400) {
        const error = await response.text();
        throw new Error(error);
      }
      throw new Error("Network response was not ok.");
    }
    
    export function handleError(error) {
      console.error("API failed. " + error);
      throw error;
    }
    
    
    export function GetCountries() {
      const url = "/countries"
      return fetch(url, {
        method: "GET",
        credentials: "include",
      }).then(handleResponse).catch(handleError);
    }
    
    export function GetCities() {
      const url = "/cities"
      return fetch(url, {
        method: "GET",
        credentials: "include",
      }).then(handleResponse).catch(handleError);
    }
    
    export function GetRegions() {
      const url = "/regions"
      return fetch(url, {
        method: "GET",
        credentials: "include",
      }).then(handleResponse).catch(handleError);
    }
    
    function getAddressStructure() {
      return Promise.all([
        GetCountries(),
        GetCities(),
        GetRegions()
      ])
        .then((response) => {
          return {
            countries: response[0],
            cities: response[1],
            regions: response[2]
          };
        })
        .catch((error) => {
          return {
            countries: response[],
            cities: response[],
            regions: response[]
          }
        });
    }
    
    
    // this is how you can use it:
              getAddressStructure()
                .then((response) => {
                  var apiResults = { };
                  apiResults = { ...response};
                  return resolve(apiResults);
                })
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 2020-09-17
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多