0

I'm passing the resolve function as a param, and I have to know if it had been called somewhere. Instead of manually maintaining a state, I'm wondering if there's a function can do it for me.

const p = new Promise((resolve, reject) => {
  f(resolve)
  g(resolve)
}

if resolve have been called in f then g should have different behavior, for example

const arr = [1,2,3]
const g = resolve => {
  if(resolve has been called) {
    do nothing 
  } else {
    const el = arr.pop()
    resolve(el)
  }
}

I'd like to have a function f(resolve) == true|false


I know there's no standard function like this yet since it's not included in the promise spec. So I'm asking for a proposal which has been implemented in the browser or a polyfill.

If both not now, I'll just leave this question open here in case that some day in the future, there comes one.

Shuumatsu
  • 592
  • 2
  • 5
  • 12
  • 1
    There's nothing special about the `resolve` function, it's just an ordinary function. So you're basically asking if there's a way to tell if a function has been called before, without coding it into the function itself. I don't think so. – Barmar Jul 07 '19 at 08:54
  • @Barmar yes, I agree with you... but `resolve` itself do maintain the state that if it has been called, and in some sense, it's kinda special, it's implemented by the browser. So I'm wondering if there is an API to query it for me. – Shuumatsu Jul 07 '19 at 09:09
  • See the linked question as a way to find out the state of the promise itself. – Barmar Jul 07 '19 at 09:13
  • @Barmar I've already checked that and that's what I mean "manually maintain a state". I know the API I'm asking for is not a standard API(since the promise spec doesn't include it). In fact, I'm asking for a proposal which has been implemented in the browser or a polyfill. – Shuumatsu Jul 07 '19 at 09:21
  • As stated in the linked question, and the question it links to, there isn't such a thing in the browser. – Barmar Jul 07 '19 at 09:25
  • 5
    I would propose that there must be a simpler way to solve your problem. This sounds like an [XY problem](http://xyproblem.info/). – deceze Jul 07 '19 at 09:34
  • The promise can be one of: pending, resolved, or rejected. If the promise is not pending, then it will be either resolved or rejected. – Dov Rine Jul 07 '19 at 09:44
  • This might help you: https://ourcodeworld.com/articles/read/317/how-to-check-if-a-javascript-promise-has-been-fulfilled-rejected-or-resolved – Dov Rine Jul 07 '19 at 09:46
  • @deceze hhhh. you are right. I will update my question description. – Shuumatsu Jul 07 '19 at 10:22
  • 1
    If you are the author of the `f` function, then surely you can let it return `true` when it has called `resolve`. What am I missing here? Or am I confused by potentially two different definitions of `f` in your question? – trincot Jul 07 '19 at 10:35
  • 1
    *"implemented in the browser"*: the Promise specification is not browser related. It is part of native EcmaScript. – trincot Jul 07 '19 at 10:41

1 Answers1

0

You can create a wrapper function that tracks invocation:

const p = new Promise((resolve, reject) => {
    const resolveContext = {
        didResolve: false
    };
    const wrappedResolve = (...args) => {
        resolveContext.didResolve = true;
        resolve(...args)
    };
    wrappedResolve.context = resolveContext;
    f(wrappedResolve)
    g(wrappedResolve)
}

Now your g function can access wrappedResolve.context.didResolve

lorefnon
  • 12,875
  • 6
  • 61
  • 93