0

I am trying get the status of the POST request outside of save_function. I am posting this dummy request to Reddit. save_function() always will get the status POST request.

How should I structure my request?

// how can I use always here?
save_function().always(reason => {
  // here I am trying to access the post call here
  console.log(reason);
})

save_function().then(data => {
  console.log("done");
});

function test() {
  return $.getJSON("https://www.reddit.com/r/gifs/.json");
}

function save_function() {
  let myFirstPromise = new Promise((resolve, reject) => {
    if (true) {
      test().done(data => {
        resolve(data);
      })
    } else {
      resolve({
        test: "test"
      });
    }
  });

  return myFirstPromise.then((data) => {
    console.log(data);
    // how do i access this  ajax call like caller_function.always()
    return $.ajax({
        method: "POST",
        url: "https://www.reddit.com/r/gifs/.json", //dummy json call 
        data: {
          "test": data.test
        }
      })
      .done(test => {
        console.log(test);
      })
      .fail(reason => {
        console.warn(reason);
      })
  });
}
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
Mr. Snuffles
  • 33
  • 2
  • 6
  • avoid the Promise constructor anti-pattern in `save_function` - since `$.getJSON` returns a promise ... https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it – Jaromanda X Aug 28 '17 at 02:42
  • this really does not answer my question. what's the alternative here? – Mr. Snuffles Aug 28 '17 at 03:28
  • It's not an answer rather a comment on your code structure. You should not create a promise because `test()` already return a promise. Instead just `if (true) {return test()} else {return {test: "test"}}` – slebetman Aug 28 '17 at 03:33
  • Never resolve/reject a promise. Just return them. – slebetman Aug 28 '17 at 03:33
  • `this really does not answer my question` that's why it's a comment – Jaromanda X Aug 28 '17 at 03:52

0 Answers0