移动返回JSON promise响应的位置

时间:2018-10-09 19:56:43

标签: javascript json

我有一些完美的代码,除了它在错误的函数内返回了代码。它在我的报表处理程序中返回,但是我需要在我的函数中使用它,因为这是我需要处理的地方。

这是我的代码:

function myfunction(ref) {
  getReport(ref, "queue", "hour", "2018-09-03", "2018-10-04", "pageviews", "page").done(function(r1) {
    getReport(r1.reportID, "get").done(r2=>reportHandler(r1.reportID, r2, 0))
    // HERE IS WHERE I WANT TO GET MY DATA RETURNED
      });
}


function reportHandler(id, r2, retries){
    if(retries >= 3){return}
    if (r2.error == "report_not_ready") {
        console.log("Not ready");
        setTimeout(function() {
          getReport(id, "get").done(r2=>reportHandler(id, r2, retries + 1))
        }, 2000);
      }
      // THIS IS NOT WHERE I WANT MY DATA BUT WHERE IT COMES OUT
      console.log(r2);
}

function getReport(ref, type, granularity, from, to, metric, element) {
  return $.getJSON("report.php", {
    ref: ref,
    type: type,
    granularity: granularity,
    from: from,
    to: to,
    metric: metric,
    element: element,
  });
}

这是我能得到的最接近的

function myfunction(ref) {
  getReport(ref, "queue", "hour", "2018-09-03", "2018-10-04", "pageviews", "page").done(function(r1) {
    getReport(r1.reportId, "get").done((r2) => {
      reportHandler(r1.reportId, r2, 0);
      console.log(r2);
    });
  });
}

如何从API将JSON响应的结果收集到“ myfunction”中,以便可以在该函数中进行处理。

2 个答案:

答案 0 :(得分:1)

您需要使用以下内容来使reportHandler散播:

const myfunction = ref =>
  getReport(ref, "queue", "hour", "2018-09-03", "2018-10-04", "pageviews", "page").then(r1 =>
    getReport(r1.reportId, "get").then(r2 =>
      reportHandler(r1.reportId, r2, 0).then(r3 => {
        console.log(r1,r2,r3);
      })
    );
  );

const reportHandler = (id, r2, retries) =>
    new Promise((resolve, reject) => {
      if(retries >= 3) { resolve() }
      if (r2.error == "report_not_ready") {
        console.log("Not ready");
        setTimeout(function() {
          return getReport(id, "get").then(r2 => reportHandler(id, r2, retries + 1)).then(r => resolve(r))
        }, 2000);
      }
    }

您有setTimeout,需要等待。看看这种方法是否对您有用。

答案 1 :(得分:1)

您可以为此使用list_of_rows=[i for i in range(12)]+[14,19] list_of_columns=[i for i in range(4)]+[6] df1=df.loc[list_of_rows,list_of_columns] / async

您可以使用await来等待它们的完成,而不是将承诺链接在一起。我添加了await函数,它“等待X毫秒”来替换您的sleep(通过使用自己的setTimeout)。

setTimeout


这里有一些工作的临时代码,它们调用JSON端点,在成功执行之前模拟一些重试。

const sleep = m => new Promise(r => setTimeout(r, m));

async function myfunction(ref) {
  var r1 = await getReport(ref, "queue", "hour", "2018-09-03", "2018-10-04", "pageviews", "page");
  var r2 = await getReport(r1.reportID, "get").then(r2 => reportHandler(r1.reportID, r2, 0));
  
  console.log(r2);
}

async function reportHandler(id, r2, retries) {
  if (retries >= 3 || r2.error != "report_not_ready") return r2;
  
  await sleep(2000);                              //Wait 2 seconds
  var report = await getReport(id, "get");        //Try again
  return reportHandler(id, report, retries + 1);  //Until we get a result
}

function getReport(ref, type, granularity, from, to, metric, element) {
  return $.getJSON("report.php", {
    ref: ref,
    type: type,
    granularity: granularity,
    from: from,
    to: to,
    metric: metric,
    element: element,
  });
}
const sleep = m => new Promise(r => setTimeout(r, m));

async function myfunction(ref) {
  var r1 = await getReport();
  var r2 = await getReport().then(r2 => reportHandler(r2, 0));
  console.log(r2);
}

async function reportHandler(r2, retries) {
  if (retries >= 3) return r2;
  console.log(`Attempt #${retries+1} failed. Retrying in 2 seconds.`);
  
  await sleep(2000);
  var report = await getReport();
  return reportHandler(report, retries + 1);
}

function getReport() {
  return $.getJSON('https://jsonplaceholder.typicode.com/todos/1');
}

myfunction();