-3

Let's say we have some Promise for we knew it resolves at some point. Now I have sync code (ag. no await possible) called repeatedly (eg. onAnimationFrame). How to use the result of the promise there, if it is fullfilled?

Like (JS pseudocode):

let promise=FetchSomething();
let updater = ()=>{
  drawSomething();
  if(promise IS FULLFILLED)  // how to know if fullfilled?
    drawThing(RESULT OF promise); // how to get the result here?
  requestAnimationFrame(updater);
}
updater();
9
  • 4
    can't you just use the .then function? E.g. FetchSomething().then((result) => drawThing(result)); Commented Sep 1, 2023 at 14:43
  • No, it will not happen at the requestAnimationFrame handler then, but at some uncontrolled point in time. Commented Sep 1, 2023 at 14:44
  • 3
    requestAnimationFrame is not synchronous Commented Sep 1, 2023 at 14:44
  • 1
    You can just put the requestAnimationFrame inside the .then. Commented Sep 1, 2023 at 14:46
  • 1
    I think in OP's use case the updater function is called many times per second, and needs to start drawing after the result of an async call. I think you could have some outside variable let result; and then FetchSomething().then((r) => result = r), and in your updater function: if(result != null) { drawThing(result) } Commented Sep 1, 2023 at 15:17

2 Answers 2

2

The easiest way is to set a variable yourself:

let promise = FetchSomething();
let done = false

/* variable will be set once promise settled */
promise.then(() => {
    done = true
})

let updater = () => {
  drawSomething();

  if (done) {  
      drawThing(/* RESULT OF promise */); 
  }

  requestAnimationFrame(updater);
}

updater();

You can do the same with the result, i.e.:

let promise = FetchSomething();
let done = false
let result = null

promise.then((value) => {
    done = true
    result = value
})

let updater = () => {
  drawSomething();

  if (done) {  
      drawThing(result); 
  }

  requestAnimationFrame(updater);
}

updater();
Sign up to request clarification or add additional context in comments.

3 Comments

This works. I just wonder if there is any easier way... I mean the Promise object for sure will store if it is resolved and what the result is, no?
@dronus I don't think there are public (documented) and accessible properties on a Promise object to get the status and result unfortunately. I've had the same problem as you can couldn't find any way of getting that information from a Promise object alone. So this leads me to believe that you have to keep track of that information yourself, even though it technically should be in the Promise object.
Actually the Chrome JavaScript console does annotate any Promise object with fullfilled or pending so the secret master seem to know... just the ordinary user isn't allowed too!
-1

Perhaps this simple promise using your names will illustrate:

// fake 
function FetchSomething(myfoo) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(myfoo);
    }, 2000);
  });
}

function drawSomething(val) {
  console.log('draw', val);
}

function drawThing(val) {
  console.log('drawthing', val);
}

function woops(val) {
  console.log(val);
}

let handleFulfilled = (val) => {
  drawSomething(val);
  drawThing(val);
  requestAnimationFrame(updater);
}

let handleRejected = (val) => {
  drawSomething(val);
  woops(val);
  requestAnimationFrame(updater);
}
let goodfoo = "good foo";

function updateFun(v) {
  let fetchPromise = FetchSomething(v);

  fetchPromise.then(handleFulfilled, handleRejected);
  let t = new Date(Date.now()).getSeconds();
  setTimeout(function() {
    updateFun(goodfoo + t)
  }, 5000);
}
updateFun(goodfoo);

5 Comments

Updated with some time to show the cycle of the execution which could be in the Fetch also...
Thing is, drawing will happen here if the promise is fullfilled. But this is not the right context - drawing has to happen when the regular drawing happens. It just also draws the result of the promise then.
But completing when the promise is fulfilled is the entire point of a promise. Then on the next process a new promise is created since the first has already been fulfilled - OR the current one can be used since it has already been fulfilled; they are fulfilled once and a new one can be created at any time you desire.
Yes, but sometimes other stuff may happen, and the result of the promise may be used there if, and only if, it has fulfilled to that point in time. The creator of the promise may share your thought, but the user (me) in this case need it's result in the way I described.
Since you appear to want to capture the value only once I think you can use what marco put up that stores the value of the one-time call to the async method but perhaps only need one (the value) variable i.e. if (!!result) { drawThing(result); } which should work due to the "falsey" nature of JavaScript on that null/not null value in result If you DO call that again, you will need to perhaps set result back to null when it instantiates the call once again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.